水平有限,如有技术问题,请提出,谢谢!!!
下面描写的是一段超级简单的doPost方法,可以使用HttpServletRequest对象里的request.getParameter()方法接收来自页面表单的数据。
比如:
request.getParameter("UserName");
这个方法表面接受页面表单名为UserName的数据。
页面:
<form method="post" action="/Film/servlet/UserRegister">
用户名:<input type="text" name="UserName"><br>
package app.user.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import core.security.MD5;
import app.user.service.UserService;
public class UserRegisterAction extends HttpServlet {
private UserService userService=new UserService();
private String userName;
private String passWord;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//JS弹出中文,解决乱码
response.setCharacterEncoding("GB2312");
userName=request.getParameter("UserName");
passWord=request.getParameter("PassWord");
PrintWriter out=response.getWriter();
if(request.getParameter("PassWord2").equals(passWord)&&userName!=null&&userName.length()!=0){
userService.saveUserInformationByRegister(userName,MD5.getMD5(passWord));
out.print("<script language='javascript'>alert('注册成功!');window.location.href='/Film/index.jsp';</script>");
}else{
out.print("<script language='javascript'>alert('填写错误!请重新填写.');window.location.href='/Film/servlet/registerUser.jsp';</script>");
}
}
}
之后,我们需要在web.xml文件里配置这个servlet。
<servlet>
<servlet-name>UserRegister</servlet-name>
<servlet-class>app.user.action.UserRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserRegister</servlet-name>
<url-pattern>/servlet/UserRegister</url-pattern>
</servlet-mapping>
引用
这里的
<servlet-name> UserRegister </servlet-name>
使用servlet-name定义servlet的名称,名称可以随意,一般选取有意义的名称。
<servlet-class>app.user.action.UserRegister</servlet-class>
servlet用来定义由哪一个类来处理用户的请求。形式为:包名+类名
<servlet-mapping>
<servlet-name>UserRegister</servlet-name>
该名称与上面定义的servlet名要一致。
<url-pattern>/servlet/UserRegister</url-pattern>
定义servlet的表现形式,也就是用户在浏览器的地址栏输入的一部分url地址。
OK,到这里这个servlet算是完成了。
首先,使你的类继承HttpServlet类,让我们来看一看HttpServlet类的原码:
package javax.servlet.http;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public abstract class HttpServlet extends GenericServlet
implements Serializable
这里可以看见HttpServlet类继承GenericServlet类,而且继承了Serializable接口,我们来看一看GenericServlet的原码:
package javax.servlet;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.ResourceBundle;
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
里面也没什么。重点还是在HttpServlet这个类。
这个类里实现了几个HTTP协议请求的方法。比如:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1"))
resp.sendError(405, msg);
else
resp.sendError(400, msg);
}
因为我们在自己的类里覆盖了doPost方法,所以这里不会向页面发 resp.sendError(405, msg);或 resp.sendError(400, msg);错误。
HttpServlet类里的service方法实现了GenericServlet基类的service抽象方法。
首先,进行request = (HttpServletRequest)req;response = (HttpServletResponse)res;
转换,成功之后,掉用service(request, response);下面代码展现了这一过程。
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try
{
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
}
之后,调用下面的service(request, response);方法。
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
long lastModified;
String method = req.getMethod();
if (method.equals("GET")) {
lastModified = getLastModified(req);
if (lastModified == -1L)
{
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if (ifModifiedSince < lastModified / 1000L * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(304);
}
}
}
else if (method.equals("HEAD")) {
lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
}
else if (method.equals("POST")) {
doPost(req, resp);
}
else if (method.equals("PUT")) {
doPut(req, resp);
}
else if (method.equals("DELETE")) {
doDelete(req, resp);
}
else if (method.equals("OPTIONS")) {
doOptions(req, resp);
}
else if (method.equals("TRACE")) {
doTrace(req, resp);
}
else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
这里可以看见,根据我们传入的请求和响应对象,判断应该执行哪个方法。