如何让用户登录?

时间:2014-02-07 14:36:59

标签: html jsp tomcat login

我正在尝试让用户在登录屏幕上输入详细信息后保持登录状态。我正在使用apache tomcat和MS access数据库。我可以使用存储在我的数据库中的用户登录。但我无法弄清楚如何让用户在我的网站上登录。我一直在阅读有关会话和尝试示例代码但我无法让它工作,我不断得到不同的错误。登录后我设法通过名称“欢迎”用户,但是如果我点击我的任何其他网页,我就无法保留欢迎用户消息。

loginJsp.jsp -

<sql:setDataSource
var = "bookdB"
scope = "session"
driver = "sun.jdbc.odbc.JdbcOdbcDriver"
url = "jdbc:odbc:bookdB"
/>

<sql:query var ="NameCheck" scope = "session" dataSource = "${bookdB}">

SELECT * FROM Users
WHERE Username = ?
AND Password = ?

<sql:param value = "${param.user}" />
<sql:param value = "${param.password}" />
</sql:query>

<c:if test = "${NameCheck.rowCount == 0}">
<jsp:forward page = "loginError.html"/>
</c:if>

<c:if test = "${NameCheck.rowCount != 0}">
<jsp:forward page = "index.jsp"/>

的index.jsp

 <sql:query dataSource="${bookdB}" var="result">
SELECT * FROM Users
WHERE Username = ?
AND Password = ?

<sql:param value = "${param.user}" />
<sql:param value = "${param.password}" />
 </sql:query>

<font color="orange" face="Futura Std-Light" size="4">         
 <c:forEach var="row" items="${result.rows}"> 
   Welcome ${row.FirstName}!
 </c:forEach>  
</font>

enter image description here


非常感谢任何帮助,谢谢!

这是对的吗?

<c:if test = "${NameCheck.rowCount == 0}">
HttpSession session = request.getSession();
session.setAttribute("connecte", "true");
    session.setAttribute("login", "${param.user}");
<jsp:forward page = "loginError.html"/>
</c:if>

1 个答案:

答案 0 :(得分:2)

在您的servlet操作处理程序中添加以下内容:

HttpSession session = request.getSession();
session.setAttribute("connecte", "true");
session.setAttribute("login", "TOM");
您在JSP中的

将其添加到页面顶部

<%
if (session.getAttribute("connecte") == null
        || !((String) session.getAttribute("connecte"))
                .equals("true")) {
    String redirectURL = "/path/ToYour/login.jsp;
    response.sendRedirect(redirectURL);
}
%>
<h1>Hello <%= session.getAttribute("login") %></h1>

如果可以,它会显示 你好汤姆

的login.jsp:

<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Login Page</title>
</head>
<body>

<form action="LoginServlet" method="post">

Username: <input type="text" name="user">
<br>
Password: <input type="password" name="pwd">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>

LoginServlet.java:

package com.journaldev.servlet.session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String userID = "Pankaj";
private final String password = "journaldev";

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    // get request parameters for userID and password
    String user = request.getParameter("user");
    String pwd = request.getParameter("pwd");

    if(userID.equals(user) && password.equals(pwd)){
        Cookie loginCookie = new Cookie("user",user);
        //setting cookie to expiry in 30 mins
        loginCookie.setMaxAge(30*60);
        response.addCookie(loginCookie);
        response.sendRedirect("LoginSuccess.jsp");
    }else{
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
        PrintWriter out= response.getWriter();
        out.println("<font color=red>Either user name or password is wrong.</font>");
        rd.include(request, response);
    }

}

}

LoginSuccess.jsp:

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                             "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Success Page</title>
</head>
<body>
<%
String userName = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("user")) userName = cookie.getValue();
}
}
if(userName == null) response.sendRedirect("login.html");
%>
<h3>Hi <%=userName %>, Login successful.</h3>
<br>
<form action="LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
</body>
</html>