jsp 登录问题

首先我创建三个jsp文件:login.jsp、login_success.jsp和login_failure.jsp

login.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
   <center>
    <h1>系统登录</h1>
    <form name="loginForm" action="<%=path %>/LoginServlet" method="post">
       用户名:<input type="text" name="username"><br>
       密  码:<input type="password" name="password"><br>
       <input type="submit" value="登录"/><br>
    </form>
    </center>
  </body>
</html>

login_success.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
   <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<body>
      <center> 
    <h1>登录成功</h1>
     <br> 
    </center> 
</body>
</html>

login_failure.jsp如下:

<%@ page language="java" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

<body> 
    <center>
        <h1>登录失败</h1>
        <br>
        <a href="<%=path %>/login.jsp">返回</a>
    </center>
</head>

  </body>
</html>

然后我分别创建Users类,UsersDAO类和LoginServlet类

Users类

package net.nw.vo;
//用户类
public class Users {
    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;
    }
    

}

UsersDAO类

package net.nw.dao;

import net.nw.vo.Users;

//用户操作类
public class UsersDAO {
    
    
    public boolean usersLogin(Users u){
        if(u.getUsername().equals("admin")&&u.getPassword().equals("123456")){
            return true;
        }else{
            return false;
        }
    }

}

LoginServlet类

package net.nw.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 net.nw.dao.UsersDAO;
import net.nw.vo.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();
u.setUsername(request.getParameter("username"));
u.setPassword(request.getParameter("password"));
UsersDAO dao=new UsersDAO();
if(dao.usersLogin(u)){
response.sendRedirect("login_success.jsp");
}else{
response.sendRedirect("login_failure.jsp");
}

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

 

最后配置服务器和发布;

我用的是tomcat7 ,MyEclipse10,但是还是出错,希望各位高手指点,谢谢!

 

 

posted @ 2014-04-24 19:50  范钰哲  阅读(176)  评论(0)    收藏  举报