1.
package com.Jasper2003.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/demo_do") public class DemoServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append("I'm doGet method!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append("I'm doPost method!"); } }
2. JSP更加适合做展示, Servlet更加适合做处理.
反例: 当用户访问demo_do时, Servlet返回给用户一个网页
@WebServlet("/demo_do") public class DemoServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"); resp.getWriter().append("<html>"); resp.getWriter().append("<head>"); resp.getWriter().append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); resp.getWriter().append("</head>"); resp.getWriter().append("<body>"); resp.getWriter().append("Hi! doGet method!"); resp.getWriter().append("</body>"); resp.getWriter().append("</html>"); }
效果:
可见, 这是非常麻烦的
正例: 利用Servlet处理注册请求
1. 利用RegisterServlet.java处理注册请求:
<1> 在RegisterServlet.java中:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); int age = Integer.parseInt(request.getParameter("age")); String gender = request.getParameter("gender"); gender = new String(gender.getBytes("iso-8859-1"), "utf-8"); boolean isSuccess = DBUtil.addUser(username, password, age, gender); if(isSuccess){ request.setAttribute("message","你注册成功了,请登录!"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ request.setAttribute("message","很遗憾, 由于用户名重复, 注册失败了, 请你重新注册!"); request.getRequestDispatcher("register.jsp").forward(request,response); } }
<2> 在register.jsp中 (将表单提交的地址修改为从register_do.jsp修改为register_do):
<form action ="register_do" method ="post">
用户名: <input type="text" name="username"/><br/>
密码: <input type="password" name="password"/><br/>
年龄: <input type="text" name="age"/><br/>
性别: 男<input type="radio" name="gender" value="男的!"/>
女<input type="radio" name="gender" value="女的!"/><br/>
<input type = "submit" value="我要注册成为新用户!"/>
</form>
效果:
正例2: 利用Servlet 完成登录请求
<1> 在LoginServlet中:
@WebServlet("/login_do")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = DBUtil.verifyAccount(username,password);
if(user == null){
request.setAttribute("message","<font color='red'>很遗憾,由于用户名或密码错误, 你登录失败了!</font>");
request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
int number = 0;
ServletContext application = this.getServletContext();
if(application.getAttribute("userNumber") != null){
number = (Integer)application.getAttribute("userNumber");
}
number++;
application.setAttribute("userNumber",number);
request.getSession().setAttribute("user",user);
request.getRequestDispatcher("personalCenter.jsp").forward(request,response);
}
}
<2> 在login.jsp中:
<form action ="login_do" method ="post">
用户名: <input type="text" name="username"/><br/>
密码: <input type="password" name="password"/><br/>
<input type = "submit" value="我要登录!"/>
</form>
效果: