gpt4 book ai didi

java - 无法使用从其他 JSP 页面引入的参数

转载 作者:行者123 更新时间:2023-12-02 07:39:52 25 4
gpt4 key购买 nike

这是我第一次尝试在2个JSP页面之间交换值,index.jsp页面有一个登录表单,login.jsp页面将验证此登录,如果登录不成功,它将重定向到index。 jsp 带有一个名为 valid 的参数,值为 0,如果成功,值为 1。

index.jsp

<%@page import="javax.enterprise.inject.spi.Bean"%>
<%@page import="myDatabase.Login"%> //this is class that I created
<%@page import="myDatabase.JavaDB"%> //this is a class that I created
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="indexCSS.css" />
<title>Chat</title>

</head>
<body>
<jsp:include page="login.jsp">
<jsp:param name="valid" value="1"/>
</jsp:include>
<% String valid = request.getParameter("valid");%>
<div class="mainPage">
<div id="header">
<div id="pageTitle">
Chat
</div>
</div>
<div id="loginBox">
<form name="login" action="login.jsp" method="POST">

<div id="loginItems">
<div id="loginTitle">
Log in
</div>
<hr style="color:green;">
<div style="margin-top:17px; overflow:hidden;">
<label for="id">
ID
</label>
<span id="idError" class="error">
<% if(valid.equals("0")) { %>is not valid<% } %>

</span>
<br>
<input class="inputText" type="text" name="id" value="" maxlength="10" autocomplete="off"/>
</div>
<div style="margin-top:17px; overflow:hidden;">
<label for="password">
Password
</label>
<span id="passwordError" class="error">

</span>
<br>
<input class="inputText" type="password" name="password" value="" maxlength="32" autocomplete="off"/>
</div>
<div style="margin-top:17px; overflow:hidden;">
<div>
Forgot your Password?
</div>
</div>
<div style="margin-top:17px; position:relative; overflow:hidden">
<input class="inputButton" type="submit" value="Log in" name="loginButton" />
</div>
</div>
</form>
</div>
</div>
</body>
</html>

这是登录.jsp

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="loginBean" scope="session" class="myDatabase.Login" />
<jsp:setProperty name="loginBean" property="id" />
<jsp:setProperty name="loginBean" property="password" />
<%
JavaDB myJavaDB=new JavaDB();
myJavaDB.Connect("IULChat","iul","iul");
if(myJavaDB.isConnected()==true){
//response.sendRedirect("index.jsp?a=2");
Login myLogin = new Login(loginBean.getId(),loginBean.getPassword());
myLogin.setConn(myJavaDB.getMyConnection());
myLogin.login(); loginBean.setId(0); loginBean.setPassword("");
if(myLogin.isValid()==true)
{
response.sendRedirect("index.jsp?valid=1");
}
else
{
response.sendRedirect("index.jsp?valid=0");
}
}
else
out.println("no");
%>

</body>
</html>

当我运行该项目时,出现此错误。

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException

root cause

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2 logs.

如果删除本节中的 java 代码,此错误就会消失

                    <span id="idError" class="error">
<% if(valid.equals("0")) { %>is not valid<% } %>
</span>

最佳答案

页面可能抛出 NullPointerException,因为“valid”的值为 null。始终使用格式

进行字符串与文字的比较
<literal>.equals(<variable>)

尝试使用以下更新的代码行:

<span id="idError" class="error">
<% if("0".equals(valid)) { %>is not valid<% } %>
</span>

关于java - 无法使用从其他 JSP 页面引入的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11746452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com