使用Servlet实现登录小例子

一 实体类
package  com.po;
//用户类
public  class  Users {
 
         private  String  username ;
         private  String  password ;
        
         public  Users()
        {
                
        }
 
         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;
        }
        
        
}
二 Servlet类
package 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;
import com.po.Users;
public class LoginServlet extends HttpServlet {
    /**
     * Constructor of the object.
     */
    public LoginServlet() {
        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 {
        Users u = new Users();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        u.setUsername(username);
        u.setPassword(password);
        //判断用户名和密码是否合法
        if(u.getUsername().equals("admin")&&u.getPassword().equals("admin"))
        {
            response.sendRedirect(request.getContextPath()+"/login_success.jsp");
        }
        else
        {
            response.sendRedirect(request.getContextPath()+"/login_failure.jsp");
        }
    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }
}
 
三 界面
1 login.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />    
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />    
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->    
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                         < form  action = "servlet/LoginServlet"  method = "post" >
                         < p  class = "main" >
                                 < label > 用户名:  </ label >
                                 < input  name = "username"  value = ""  />
                                 < label > 密码:  </ label >
                                 < input  type = "password"  name = "password"  value = "" >    
                         </ p >
                         < p  class = "space" >
                                 < input  type = "submit"  value = "登录"  class = "login"  style =" cursor :  pointer ;" />
                         </ p >
                         </ form >
                 </ div >
         </ div >
         </ body >
</ html >
2 login_success.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />    
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />    
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->    
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                   <%
                     String loginUser =  "" ;
                      if (session.getAttribute( "loginUser" )!= null )
                     {
                        loginUser = session.getAttribute( "loginUser" ).toString();
                     }
                   %>
                     欢迎您 < font  color = "red" > <%= loginUser %> </ font > ,登录成功!
                 </ div >
         </ div >
         </ body >
</ html >
3 login_failure.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />    
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />    
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->    
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                     登录失败!请检查用户或者密码! < br >
                   < a  href = "login.jsp" > 返回登录 </ a >   
                 </ div >
         </ div >
         </ body >
</ html >
四 测试


 

 

  • 大小: 89.6 KB
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值