Servlet就是一个中间的容器来连接Client和Server的。其中比较重要的方法就是doGet和doPost方法了,这两个方法是分别用于获取传递的参数的,千万要注意,获取参数的方法是request的getParameter。就是注意这一点,还有就是设置字体的时候,一定要在getWriter之前,要不然就不能正确的设置字体了。
贴出一个简单的传递参数的例子吧:
这是一个简单的HTML界面:post.html
<!DOCTYPE html>
<html>
<head>
<title>post.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="./styles.css">-->
</head>
<body>
<form name="myfrom" id="myform" method="post" action="../servlet/methodpost">
<table>
<tr>
<td>
name:
</td>
<td>
<input type="text" name="name" id="name" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" id="submit" value="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
下面是MethodPost.java
package chapter4;
import java.io.IOException;
public class MethodPost extends HttpServlet {
/**
* Constructor of the object.
*/
public MethodPost() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
String name = request.getParameter("name"); //
String nameTemp = (String)request.getAttribute("name");
out.println("name = " + name + "|||| nameTemp = " + nameTemp);
out.println("<br />");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}