的login.html
此文件包含2个按钮的代码。登录和注册按钮是2种形式..
的 connected.jsp
此文件包含用户可以单击的按钮的代码
并注销。
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是一个打开和关闭与数据库连接的类,检查登录是否正常,并在注册的情况下注册用户
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”) 问题是它不工作。总是添加login.html ..我甚至尝试添加抛出异常(if 1 == 1是因为抛出了异常...)但是输出总是相同的(异常永远不会起作用) 有什么想法吗?
答案 0 :(得分: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) {
...
}