登录用户的信息在返回JSP主页后消失
在登录后的以下代码中,用户可以查看自己的信息,然后单击添加/删除或编辑按钮,并重定向到相应的页面。但在他编辑信息并按下“查看菜单”按钮后,他会被重定向回去,但他的信息不再可见,只是一个表格的标题。从上到下的第一张图片是用户登录时的视图,第二张是用户添加新菜单时的视图,第三张是用户再次进入主屏幕时的视图。如何在用户添加信息后更新“查看菜单”登录用户的信息在返回JSP主页后消失,jsp,redirect,login,Jsp,Redirect,Login,在登录后的以下代码中,用户可以查看自己的信息,然后单击添加/删除或编辑按钮,并重定向到相应的页面。但在他编辑信息并按下“查看菜单”按钮后,他会被重定向回去,但他的信息不再可见,只是一个表格的标题。从上到下的第一张图片是用户登录时的视图,第二张是用户添加新菜单时的视图,第三张是用户再次进入主屏幕时的视图。如何在用户添加信息后更新“查看菜单” <%@ page language="java" import="java.sql.*" import="java.text.*" errorPage=
<%@ page language="java" import="java.sql.*" import="java.text.*" errorPage="" %>
<%
String myname = (String)session.getAttribute("username");
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
ResultSet rsLoginFunction = null;
PreparedStatement psLoginFunction=null;
String sUserID=request.getParameter("username");
String sPassword=request.getParameter("password");
String message="User login successfully ";
try{
String sql = "SELECT menu.name, menu.info, menu.price FROM menu INNER JOIN clients ON menu.username = clients.username where clients.username=? and clients.password=?";
psLoginFunction=conn.prepareStatement(sql);
psLoginFunction.setString(1,sUserID);
psLoginFunction.setString(2,sPassword);
rsLoginFunction=psLoginFunction.executeQuery();
session.setAttribute("username", sUserID);
if(rsLoginFunction.next())
{
String username=rsLoginFunction.getString("menu.name")+" "+rsLoginFunction.getString("menu.info");
}
else
{
message="No user or password matched" ;
response.sendRedirect("Login.jsp?error="+message);
}
}
catch(Exception e)
{
e.printStackTrace();
}
/// close object and connection
try{
if(psLoginFunction!=null){
psLoginFunction.close();
}
if(rsLoginFunction!=null){
rsLoginFunction.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
这里是客户信息页面
<%@ include file="include/common.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
function del() {
if (confirm("Do You Want to Delete this Menu?")) {
} else {
return false;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="images/style.css" type="text/css"
charset="utf-8" />
</head>
<body>
<%
menu_slno1 = request.getParameter("menu_slno");
if (menu_slno1 != null)
menu_slno = Integer.parseInt(menu_slno1);
delete_menu = request.getParameter("delete_menu");
if ("yes".equals(delete_menu)) {
MenuId = request.getParameter("MenuId");
x = stmt1
.executeUpdate("Delete from menu where MenuId="
+ MenuId);
}
%>
<center><h2>VIEW MENU</h2></center>
<center><table width="736" height="97" border="1"></center>
<%
if (x == 1) {
%>
<tr bgcolor="gray">
<th height="35" colspan="9"><div align="center">
Menu deleted successfully!
</div></th>
</tr>
<%
}
%>
<tr bgcolor="gray">
<td><div align="center">
<strong>Menu ID</strong>
</div></td>
<td><div align="center">
<strong>Name </strong>
</div></td>
<td><div align="center">
<strong>Info</strong>
</div></td>
<td><div align="center">
<strong>Price</strong>
</div></td>
<td colspan="2"><div align="center">
<strong>Action</strong>
</div></td>
</tr>
<%
String sUserID=request.getParameter("username");
session.setAttribute("username", sUserID);
int icount = 0;
rs = stmt.executeQuery("SELECT menu.menuID, menu.name, menu.info, menu.price, menu.RestaurantID FROM menu INNER JOIN clients ON menu.username = clients.username where menu.username='" +sUserID+ "'");
while (rs.next()) {
//menu_slno = rs.getInt("menu_slno");
MenuId = rs.getString("MenuId");
%>
<tr>
<td><div align="center"><%=++icount%></div></td>
<td><%=rs.getString("Name")%></td>
<td><%=rs.getString("Info")%></td>
<td><%=rs.getDouble("Price")%></td>
<td><div align="center">
<a href="edit_menu.jsp?MenuId=<%=MenuId%>">Edit</a>
</div></td>
<td><div align="center">
<a
href="view_menu.jsp?delete_menu=yes&MenuId= <%=MenuId%>&MenuId=<%=MenuId%>"
onclick="return del()">Delete</a>
</div></td>
</tr>
<%
}
%>
</table>
<a href="add_menu.jsp">Add Menu</a>
</body>
</html>
函数del(){
如果(确认(“是否要删除此菜单?”){
}否则{
返回false;
}
}
查看菜单
菜单删除成功!
菜单ID
名称
信息
价格
行动
以及用户添加新信息的最后一页(不编辑或删除,只添加新信息)
功能验证(菜单){
//setAttribute(“用户”,“用户”);
//字符串user=request.getAttribute(“用户”);
//while(username.equals(sUserID))
if(menu.Name.value.length==0){
警告(“请输入菜单名!”);
menu.Name.focus();
返回false;
}
如果(menu.Info.value.length==0){
警告(“请输入菜单信息!”);
menu.Info.focus();
返回false;
}
if(menu.Price.value.length==0){
警告(“请输入价格!”);
menu.Price.focus();
返回false;
}
返回true;
}
添加菜单
菜单名
菜单信息
菜价
如果需要,最后一页是添加信息的功能:
<%@ include file="include/common.jsp" %>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
body>
<%
String myname = (String)session.getAttribute("username");
MenuId = request.getParameter("MenuId");
Name = request.getParameter("Name");
Info = request.getParameter("Info");
RestaurantID = request.getParameter("RestaurantID");
menu_price1 = request.getParameter("Price");
if (menu_price1 != null || !"".equals(menu_price1))
Price = Double.parseDouble(menu_price1);
pstmt = con
.prepareStatement("Insert into menu(MenuId,Name,Info,Price,RestaurantID) values(?,?,?,?,?)");
pstmt.setString(1, MenuId);
pstmt.setString(2, Name);
pstmt.setString(3, Info);
pstmt.setDouble(4, Price);
pstmt.setString(5, myname);
pstmt.executeUpdate();
con.close();
session.setAttribute("menu", "Menu added successfully");
response.sendRedirect("add_menu.jsp");
%>
</body>
</html>
在此处插入标题
正文>
问题似乎在于您的初始JSP是以两种不同的方式启动的:
1) 当用户登录时,JSP传递了用户凭据,如下所示:
String sUserID=request.getParameter("username");
String sPassword=request.getParameter("password");
2) 但是,当用户保存他/她的编辑时,我看不到它们被传递回初始JSP。他们也不应该这样。在页面之间传递凭据不是一个好主意
我的建议是重新考虑您的身份验证逻辑。通常,这是在应用程序级别管理的,而不是基于每页。因此,虽然我不建议将此用于生产代码,但您可能可以避免以下情况:
- 成功登录后,在会话中保存用户凭据
- 首先检查会话的凭据。如果它们不存在,则检查请求参数
String sUserID=request.getParameter("username");
String sPassword=request.getParameter("password");