国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > Servlet登陆功能的实现

Servlet登陆功能的实现

来源:程序员人生   发布时间:2014-09-19 18:54:49 阅读次数:2954次

(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)运行结果:





生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生