servlet验证登录用户名和密码实例项目结构
登录页面login.html
servlet验证通过后,欢迎页面
登录页面login.html:
<body> <form action="LoginCheck" method="post"> 用户名:<input type="text" name="username" /><br> 密码:<input type="text" name="password" /> <br><input type="submit" value="登陆" /> </form> </body>
web.xml文件部署servlet:
<servlet> <servlet-name>LoginCheck</servlet-name> <servlet-class>com.my.servlet.LoginCheck</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginCheck</servlet-name> <url-pattern>/LoginCheck</url-pattern> </servlet-mapping>
LoginCheck.java (servlet)
package com.my.servlet; 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;public class LoginCheck extends HttpServlet {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(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String name=request.getParameter("username"); String password=request.getParameter("password"); 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>"); if("www.xin126.cn".equals(name)&&"123".equals(password)) { out.print("欢迎你"+name); } else { out.print(" 用户名或密码不对! "); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/9004.html