程序员人生 网站导航

Servlet登陆功能的实现

栏目:互联网时间:2014-09-19 18:54:49

(1)创建动态的Login页面

//登陆界面 package com.lc; import javax.servlet.http.*; import java.io.*; public class Login extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res){ //业务逻辑 try{ //解决中文乱码 res.setContentType("text/html;charset=gbk"); PrintWriter pw = res.getWriter(); //返回登陆界面 pw.println("<html>"); pw.println("<body>"); pw.println("<h1>用户登录</h1>"); pw.println("<form action=logincl method=post>"); //action的是servlet的url pw.println("用户名:<input type=text name=username><br/>"); pw.println("密码:<input type=password name=passwd><br/>"); pw.println("<input type=submit value=loing><br/>"); pw.println("</form>"); pw.println("</body>"); pw.println("</html>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } }
(2)创建接受Login的servlet

//登陆界面 package com.lc; import javax.servlet.http.*; import java.io.*; public class Logincl extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res){ //业务逻辑 try{ //解决中文乱码 //接受用户名和密码 String u = req.getParameter("username"); String p = req.getParameter("passwd"); //验证 if(u.equals("lc") && p.equals("123")) { //合法的情况 跳转到Welcome res.sendRedirect("welcome");//这里跳转的仍是servlet的url } else { //不合法的情况 //跳转 res.sendRedirect("login");//跳转的是要到servlet的url } } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } }

(3)登陆成功要跳转的welcome页面

//登陆界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class Welcome extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res){ //业务逻辑 try{ //解决中文乱码 res.setContentType("text/html;charset=gbk"); PrintWriter pw = res.getWriter(); pw.println("hello welcome!"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } }

(4)配置文件web.xml的编写

<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <!--JSP servlet mappings starts--> <servlet> <servlet-name>Login</servlet-name> <servlet-class>com.lc.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>Logincl</servlet-name> <servlet-class>com.<span style="font-family:Arial, Helvetica, sans-serif;">lc</span>.Logincl</servlet-class> </servlet> <servlet-mapping> <servlet-name>Logincl</servlet-name> <url-pattern>/logincl</url-pattern> </servlet-mapping> <servlet> <servlet-name>Welcome</servlet-name> <servlet-class>com.lc.Welcome</servlet-class> </servlet> <servlet-mapping> <servlet-name>Welcome</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <!--JSP servlet mappings end--> </web-app>

(5)运行结果:





------分隔线----------------------------
------分隔线----------------------------

最新技术推荐