eclipse 使用jsp和session创建一个简单的登录页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18214670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 21:51:25  来源:igfitidea点击:

Create a simple Login page using jsp and session

eclipsejspsessiontomcatjavabeans

提问by insanity

I have created a simple login page in which user will give an username and password , and then it will be stored in session. After clicking on submit button it will show welcome user or the name. And if the user waits for few seconds then the session will expire and it automatically return back to the login page.

我创建了一个简单的登录页面,用户将在其中提供用户名和密码,然后将其存储在会话中。单击提交按钮后,它将显示欢迎用户或名称。如果用户等待几秒钟,则会话将过期并自动返回登录页面。

Here is my login page

这是我的登录页面

<%@  page import="java.io.*,java.util.*" language="java" contentType="text/html;                      
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean  id="user" class="user.LoginUser" scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>login</title>
</head>
<body>

<h1><center>Give your login details</center></h1>
<form method="post" action="check.jsp">
Username:<input type="text" name="username" size="20" value="<%=user.getUser() %>" >       <br>
Password:<input type="password" name="password" size="20" value=<%=user.getPassword()   %> ><br>
<input type="submit">
</form>

</body>
</html>

now in check.jsp i am doing my checking part for username and password

现在在 check.jsp 中,我正在检查用户名和密码

<%@  page import="java.io.*,java.util.*"  language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean  id="user" class="user.LoginUser" scope="session"></jsp:useBean>
<jsp:setProperty name="user" property="*"/> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>login checking</title>
</head>
<body>
<%

 String USER=user.getUsername();
 int PASSWORD=user.getPassword();
 if(USER.equals("abhirup"))
 {
 if(PASSWORD==54321)
 {
     pageContext.forward("display.jsp");
 }
 else
 {
     out.println("Wrong password");
     pageContext.include("login.jsp");
 }
 pageContext.include("login.jsp");

 }



%>

</body>
</html>

and then finally i am displaying it in display.jsp

然后最后我在 display.jsp 中显示它

<%@ page import="java.io.*,java.util.*" page language="java" contentType="text/html;  charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean  id="user" class="user.LoginUser" scope="session" ></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Display</title>
</head>
<body>
<% String title="Welcome : successful login";
out.println(title);%>
<h3><center>Your Name:Abhirup Parui</center></h3><br>
Username<%=user.getUsername()%><br>
<%session.setMaxInactiveInterval(20);
pageContext.include("login.jsp");
%>
</body>
</html>

and also this is my LoginUser.java file

这也是我的 LoginUser.java 文件

package user;

public class LoginUser {
String username;
int password;

    public void setUsername(String value)
    {
            username=value;
    }
    public void setPassword(int value)
    {
        password=value;
    }
public String getUsername(){return username;}
public int getPassword(){return password;}

}

I am using Eclipse IDE and Tomcat server. Eclipse has shown not a single error in any of the pages but still when i run my login.jsp page.

我正在使用 Eclipse IDE 和 Tomcat 服务器。Eclipse 没有在任何页面中显示一个错误,但在我运行 login.jsp 页面时仍然显示。

I am getting this error on running login.jsp

我在运行 login.jsp 时遇到此错误

i have followed this link

我已经按照这个链接

please help me to find my errors.

请帮我找出我的错误。

Update

更新

I can successfully run my login page. I am getting this error now, but could not figure out where is the error.last part of the error is this

我可以成功运行我的登录页面。 我现在收到此错误,但无法弄清楚错误在哪里。错误的最后一部分是这个

how to fix these error . help

如何修复这些错误。帮助

采纳答案by clav

Because you're trying to access login.jsp directly from the browser you have to move it out of the WEB-INF directory - files in WEB-INF are not publicly accessible. If you move login.jsp up one directory and enter http://localhost:8088/abhirup/login.jspin your browser it should pull up the page. However, it is a fairly common practice to put jsp pages under WEB-INF/jsp or something similar and use a servlet to intercept and process requests and then have the servlet forward to the appropriate jsp page.

因为您试图直接从浏览器访问 login.jsp,所以您必须将它移出 WEB-INF 目录 - WEB-INF 中的文件不可公开访问。如果将 login.jsp 上移一个目录并http://localhost:8088/abhirup/login.jsp在浏览器中输入,它应该会拉出页面。但是,将jsp 页面放在WEB-INF/jsp 或类似的东西下并使用servlet 拦截和处理请求,然后让servlet 转发到适当的jsp 页面是一种相当普遍的做法。

You have a syntax error on line 1, column 46 of display.jsp because you have the word pagebefore your languagedeclaration. Change this:

您在 display.jsp 的第 1 行第 46 列有语法错误,因为您pagelanguage声明之前有这个词。改变这个:

<%@ page import="java.io.*,java.util.*" page language="java" contentType="text/html;  charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

to this:

对此:

<%@ page import="java.io.*,java.util.*" language="java" contentType="text/html;  charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

回答by Reneesh

I also tried the same code and I found some error in two JSP files

我也尝试了相同的代码,但在两个 JSP 文件中发现了一些错误

My login.jsp corrected code is as given below:

我的 login.jsp 更正代码如下:

<%@  page import="java.io.*,java.util.*" language="java" contentType="text/html;                      
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@  page import="user.LoginUser"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean  id="user" class="user.LoginUser" scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Login page</title>
</head>
<body>

<h1><center>Give your login details</center></h1>
<form method="post" action="check.jsp">
User name:<input type="text" name="username" size="20" value="<%=user.getUsername() %>"><br>
Password:<input type="password" name="password" size="20" value="<%=user.getPassword()%>" ><br>
Submit <input type="submit">
</form>

</body>
</html>

Corrected check.jsp code is as follows:

更正的check.jsp代码如下:

<%@  page import="java.io.*,java.util.*"  language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@  page import="user.LoginUser"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">


<jsp:useBean  id="user" class="user.LoginUser" scope="session"></jsp:useBean>
<jsp:setProperty name="user" property="*"/> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login checking</title>
</head>
<body>
<%

String USER=user.getUsername();
String PASSWORD=user.getPassword();
if(USER.equals("admin"))
    {
if(PASSWORD.equals("admin"))
    {
pageContext.forward("display.jsp");
    }
else
    {
out.println("Wrong password");
pageContext.include("login.jsp");
    }
pageContext.include("login.jsp");
}
%>

</body>
</html>

Corrected display.jsp code:

更正的 display.jsp 代码:

<%@ page import="java.io.*,java.util.*" language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@  page import="user.LoginUser"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean  id="user" class="user.LoginUser" scope="session" ></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Display</title>
</head>
<body>
<% String title="Welcome : Successful Login";
out.println(title);%>
<h3> <center> Your Name : Reneesh </center> </h3><br>
User name : <%=user.getUsername()%><br>
<%session.setMaxInactiveInterval(20);
%>
</body>
</html>

My Java file LoginUser.java corrected code is as follows:

我的 Java 文件 LoginUser.java 更正代码如下:

package user;

public class LoginUser {
String username;
String password;

public void setUsername(String value)
    {
        username=value;
    }

public void setPassword(String value)
    {
    password=value;
    }

public String getUsername()
    {
    return username;
    }

public String getPassword()
    {
    return password;
    }

 }

Kindly try with this code, I made some changes in the code by assigning String valuue for Password. I also used Eclipse juno IDE and Apache Tom Cat v 7.0 for running this Dynamic web project. Hope you will try and let me know if there is further error.

请尝试使用此代码,我通过为密码分配字符串值对代码进行了一些更改。我还使用 Eclipse juno IDE 和 Apache Tom Cat v 7.0 来运行这个动态 Web 项目。希望你能尝试让我知道是否有进一步的错误。