jsp 包括不起作用
jsp include does not work
login.html
此文件包含 2 个 buttons.The 登录和注册按钮的代码,它们是 2 个表单..
connected.jsp
此文件包含 1 个按钮的代码,用户可以单击该按钮
并注销。
Controller.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="something.*" %>
<%@ page errorPage="Error.jsp" %>
<%
request.setCharacterEncoding("ISO-8859-7");
String errorMessages = "";
String username = request.getParameter("username");
String password = request.getParameter("password");
if ((password.length() > 0) && !(username.length() > 0)) {
errorMessages = ("message1");
throw new Exception(errorMessages);
}
if ((username.length() > 0) && !(password.length() > 0)) {
errorMessages = ("message2");
throw new Exception(errorMessages);
}
if (!(password.length() > 0) && !(username.length() > 0)) {
errorMessages = ("message3");
throw new Exception(errorMessages);
}
DB_something db = new DB_something ();
db.open();
if (request.getParameter("FistName") == null) {
db.authenticateUser(username, password);
session.setAttribute("login_status", "connected");
} else {
String fName = request.getParameter("FistName");
String lName = request.getParameter("LastName");
String email = request.getParameter("email");
String gender = request.getParameter("sex");
String month = request.getParameter("month");
String day = request.getParameter("day");
String year = request.getParameter("year");
String address = request.getParameter("Adress");
db.registerUser(fName, lName, email, username, password, gender, month, day, year, address);
session.setAttribute("login_status", "connected");
}
db.close();
%>
<jsp:forward page="index.jsp" />
DB_something 是一个 class,它打开和关闭与数据库的连接并检查登录是否正常,并在注册时注册用户
index.jsp
<%
if (request.getParameter("login_status") == null) {
%>
<jsp:include page="login.html" />
<%
} else {
if(1==1)
throw new Exception("error...");
%>
<jsp:include page="connected.jsp" />
<%
}
%> . . . .
在我的 index.jsp 中,如果用户未连接(login_status = null),我尝试包含 login.html,如果用户已连接,则包含 connected.jsp (login_status = "connected")
问题是它不是 working.it 总是添加 login.html..我什至尝试添加抛出异常(如果 1==1 是因为否则抛出异常..)但输出总是相同(异常永远不起作用)
有什么想法吗?
您在会话中保存属性,但试图从请求中提取它。
而不是指令 -
<jsp:forward page="index.jsp" />
使用这个:
<jsp:forward page="index.jsp">
<jsp:param name="login_status" value="connected" />
</jsp:forward>
或者从会话中提取属性:
if(request.getSession().getAttribute("login_status") == null) {
...
}
login.html
此文件包含 2 个 buttons.The 登录和注册按钮的代码,它们是 2 个表单..
connected.jsp
此文件包含 1 个按钮的代码,用户可以单击该按钮
并注销。
Controller.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="something.*" %>
<%@ page errorPage="Error.jsp" %>
<%
request.setCharacterEncoding("ISO-8859-7");
String errorMessages = "";
String username = request.getParameter("username");
String password = request.getParameter("password");
if ((password.length() > 0) && !(username.length() > 0)) {
errorMessages = ("message1");
throw new Exception(errorMessages);
}
if ((username.length() > 0) && !(password.length() > 0)) {
errorMessages = ("message2");
throw new Exception(errorMessages);
}
if (!(password.length() > 0) && !(username.length() > 0)) {
errorMessages = ("message3");
throw new Exception(errorMessages);
}
DB_something db = new DB_something ();
db.open();
if (request.getParameter("FistName") == null) {
db.authenticateUser(username, password);
session.setAttribute("login_status", "connected");
} else {
String fName = request.getParameter("FistName");
String lName = request.getParameter("LastName");
String email = request.getParameter("email");
String gender = request.getParameter("sex");
String month = request.getParameter("month");
String day = request.getParameter("day");
String year = request.getParameter("year");
String address = request.getParameter("Adress");
db.registerUser(fName, lName, email, username, password, gender, month, day, year, address);
session.setAttribute("login_status", "connected");
}
db.close();
%>
<jsp:forward page="index.jsp" />
DB_something 是一个 class,它打开和关闭与数据库的连接并检查登录是否正常,并在注册时注册用户
index.jsp
<%
if (request.getParameter("login_status") == null) {
%>
<jsp:include page="login.html" />
<%
} else {
if(1==1)
throw new Exception("error...");
%>
<jsp:include page="connected.jsp" />
<%
}
%> . . . .
在我的 index.jsp 中,如果用户未连接(login_status = null),我尝试包含 login.html,如果用户已连接,则包含 connected.jsp (login_status = "connected") 问题是它不是 working.it 总是添加 login.html..我什至尝试添加抛出异常(如果 1==1 是因为否则抛出异常..)但输出总是相同(异常永远不起作用) 有什么想法吗?
您在会话中保存属性,但试图从请求中提取它。
而不是指令 -
<jsp:forward page="index.jsp" />
使用这个:
<jsp:forward page="index.jsp">
<jsp:param name="login_status" value="connected" />
</jsp:forward>
或者从会话中提取属性:
if(request.getSession().getAttribute("login_status") == null) {
...
}