Servlet的温习
Servlet的复习。
温习一下Servlet的应用:
一、Servlet在web.xml中的配置:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>Login</servlet-name> <servlet-class>com.servlet.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/servlet/Login</url-pattern> </servlet-mapping>
注:<url-pattern>的取值很重要,在表单中要用到。
二、Servlet类如下所示:
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class Login extends HttpServlet { /** * Constructor of the object. */ public Login() { 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 { doPost(request,response); } /** * 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 { String nextPage = ""; HttpSession session = request.getSession(); AccountBean account = new AccountBean(); String userName = request.getParameter("username"); String pwd = request.getParameter("pwd"); account.setUsername(userName); account.setPassword(pwd); if(account.getUsername().equals("hongboliu") && account.getPassword().equals("654321")){ System.out.println(); session.setAttribute("account", account); nextPage = "http://www.google.com.hk"; }else{ nextPage = "http://www.baidu.com"; } response.sendRedirect(nextPage); return ; } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { System.out.println("init method"); } }
三、 AccountBean的设计如下:
package com.servlet; public class AccountBean { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
四、Form表单内容如下:
<form action="servlet/Login" method="post"> username:<input type="text" name="username"><br> password:<input type="password" name="pwd"><br> <input type="submit" value=" 确 定 "> </form>
注:form 中action的取值要与web.xml中<url-pattern>的值一致。
五、完成了。
1 楼
bo_hai
2011-02-14
限于时间,只能这样简单的温习一下了。