Servlet 中如何让HTML页面内容原样输出 写啦一个留言板的工程,html文件用IE打开显示的是CSS中居中的样式还有其他一些样式。但是访问servlet时显示的不居中,而且和css中定义的样式有很大差别。怎么才能在servlet中原样输出HTML 文件。附有截图
截图.doc (667 KB)
- 2012-9-21 00:05, 下载次数: 143
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yinhe.yufei.DTO;
import com.yinhe.yufei.MessageBoard;
public class ServletClass extends HttpServlet
{
protected void doGet(HttpServletRequest request , HttpServletResponse response)
{
this.doPost(request , response);
}protected void doPost(HttpServletRequest request , HttpServletResponse response)
{
int i = 1;try {
request.setCharacterEncoding(“utf-8″);
response.setContentType(“text/html;charset=utf-8″);
PrintWriter out = response.getWriter();
MessageBoard mb = new MessageBoard();out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>留言板工程</title>”);
out.println(” <meta http-equiv=’keywords’ content=’keyword1,keyword2,keyword3′>”);
out.println(“<meta http-equiv=’description’ content=’this is my page’>”);
out.println(“<meta http-equiv=’content-type’ content=’text/html; charset=UTF-8′>”);
out.println(“<link rel=’stylesheet’ type=’text/css’ href=’css/index.css’>”);
out.println(” </head>”);
out.println(“<body >”);
out.println(“<div id=’logo’>留言板</div>”);
out.println(“<div id=’main’>”);List<DTO> list = mb.Display();
for(DTO dto : list)
{
out.println(“<div id=’msg’>”);
out.println(“<div class=’first’>”);
out.println(“<div class=’floor’>第”+(i++)+”楼</div>”);
out.println(“<div class=’name’>”+dto.getUsername()+”</div>”);
out.println(“</div>”);
out.println(“<div id=’content’>”+dto.getContent()+”</div>”);
out.println(“<div class=’first’>”);
out.println(“<div class=’floor’>”+dto.getPubtime()+”</div>”);
out.println(“<div class=’name’>”+dto.getIp()+”</div>”);
out.println(“</div>”);
out.println(“</div>”);}
out.println(“<div id=’form’>”);
out.println(“<form action=’addmsg’ method=’post’>”);
out.println(“<div class=’username’><input type=’text’ name=’username’>用户名</div><br/>”);
out.println(“<div class=’p'>请输入留言</div><br>”);
out.println(“<div class=’content’><textarea cols=’60′ rows=’8′ name=’content’></textarea></div><br/>”);
out.println(“<div class=’submit’><input type=’submit’ value=’ 提 交 ’></div> “);
out.println(“</form>”);
out.println(“</div>”);
out.println(“</div>”);
out.println(” </body>”);
out.println(“</html>”);} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是Servlet类的代码<!DOCTYPE html>
<html>
<head>
<title>messageboard.html</title>
<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”this is my page”>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″><link rel=”stylesheet” type=”text/css” href=”./css/index.css”>
</head><body >
<div id=”logo”>留言板</div>
<div id=”main”>
<div id=”msg”><div>
<div>第一楼</div>
<div>撒</div>
</div><div id=”content”> 发挥年能否泥人年 饿 </div>
<div>
<div>2012.09.17</div>
<div>202.13.242.45</div>
</div>
</div>
<div id=”form”>
<form action=”board” method=”post”>
<div><input type=”text” name=”username”>用户名</div><br/>
<div>请输入留言</div><br>
<div><textarea cols=”60″ rows=”8″ name=”content”></textarea></div><br/>
<div><input type=”submit” value=” 提 交 ”></div>
</form>
</div>
</div><div id=”bottom”></div>
</body>
</html>
这是HTML源代码@CHARSET “UTF-8″;
* { margin:0px auto;
font-size:12px;
font-family:”宋体”;
}
body { background:url(../image/background.gif) center 0px repeat-x; }
#main{
width:980px;height:100%;
border:1px solid gray;
margin:0px auto;
}
#logo{
width:100%px;
height:50px;
background-image:url(../image/textured_04.png);
text-align:center;
font-size:35px;
}
#msg{
width:980px;border-bottom: 2px solid purple;
}
#content{
width:980px;
height:150px;
font-size:16px;
text-indent: 2em;
background-image:url(../image/textured_05.png);
}
.first{
width:980px;
height: 25px;
padding-top:10px;
text-align:center;
}
.floor{float:left;padding-left:20px;font-size:14px;}
.name{float:right;padding-right:20px;font-size:14px;}#form{
width:980px;lize-height:30px;
background-image:url(../image/textured_01.png);
}
.username{
color:#9400D3;
font-size:20px;
line-height:14px;}
.p{
color:#FF8C00;
font-size:20px;
}
textarea{
font-size:16px;
line-height:20px;
}
input{
color:ADFF2F;
font-size:20px;}
这是CSS样式<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list><servlet>
<servlet-name>liuyan</servlet-name>
<servlet-class>com.yinhe.servletpackage.ServletClass</servlet-class>
</servlet>
<servlet>
<servlet-name>AddMessageServlet</servlet-name>
<servlet-class>com.yinhe.servletpackage.AddMessageServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>YzmServlet</servlet-name>
<servlet-class>yzmservlet.YzmServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>yzmservlet.LoginServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>liuyan</servlet-name>
<url-pattern>/board</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddMessageServlet</servlet-name>
<url-pattern>/addmsg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>YzmServlet</servlet-name>
<url-pattern>/yzm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
这是web.xmlhttp://bbs.51cto.com/thread-958892-1-1.html