0

在此处输入图像描述在此处输入图像描述在此处输入图像描述

在以下代码中登录后,用户可以查看自己的信息,然后单击添加/删除或编辑按钮并重定向到相应的页面。但是在他编辑信息并按下“查看菜单”按钮后,他被重定向回来,但他的信息不再可见,只是表格的标题。从上到下的第一张图片是用户登录时的视图,第二张是他添加新菜单时的视图,第三张是当他再次进入主屏幕时发生的情况。用户添加信息后如何更新“查看菜单”。

<%@ 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>

以及用户添加新信息的最后一页(不编辑或删除只是添加新信息)

<%@ 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>
<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" />

<script language="javascript">
function validate(menu) {


//  request.setAttribute("user","<username>");
//  String user=request.getAttribute("user");
//  while (username.equals(sUserID))



        if (menu.Name.value.length == 0) {
        alert("Please enter Menu Name!");
        menu.Name.focus();
        return false;
    }

    if (menu.Info.value.length == 0) {
        alert("Please enter Menu Info!");
        menu.Info.focus();
        return false;
    }

    if (menu.Price.value.length == 0) {
        alert("Please enter price!");
        menu.Price.focus();
        return false;


    }

    return true;

}
</script>


</head>
<body>

<h2>ADD MENU</h2>
<form name="menu" id="menu" action="db_add_menu.jsp"
    method="post" onSubmit="return validate(this)">
    <table width="332" height="252" border="0" align="center"
        cellpadding="2" cellspacing="2">
        <tr>
            <th height="33" colspan="2"><div align="center">
                    <%
                        menu = (String)     session.getAttribute("menu");
                        session.removeAttribute("menu");
                        if (menu != null)
                            out.print(menu);



                    %>
                </div></th>
        </tr>

        <tr>
            <th>Menu Name</th>
            <td><input name="Name" type="text" class="text"
                id="Name" value="" /></td>
        </tr>
        <tr>
            <th>Menu Info</th>
            <td><input name="Info" type="text" class="text"
                id="Info" value="" /></td>
        </tr>
        <tr>
            <th>Menu Price</th>
            <td><input name="Price" type="text" class="text"
                id="Price" value="" /></td>
        </tr>

        <tr>
            <td class="submission" colspan="2"><div align="center">
                    <input name="s" type="submit" class="button" value="ADD" /> <input
                        name="Reset" type="reset"     class="button" value="RESET" />
                </div></td>
        </tr>
        <tr>
            <th class="submission" colspan="2"><div align="center">
                    <a href="view_menu.jsp">View Menu</a>
                </div></th>
        </tr>
    </table>

</form>


</body>
</html>

如果您需要,最后一页是添加信息的功能:

<%@ 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>
4

1 回答 1

0

问题似乎是您的初始 JSP 以两种不同的方式启动:

1) 当用户登录时,JSP 传递了用户凭据,如下所示:

String sUserID=request.getParameter("username");
String sPassword=request.getParameter("password");

2) 但是,当用户保存他/她的编辑时,我没有看到它们传递回初始 JSP。他们也不应该。在页面之间传递凭据不是一个好主意。

我的建议是重新考虑您的身份验证逻辑。通常这是在应用程序级别进行管理,而不是在每页的基础上进行管理。因此,虽然我不建议将此用于生产代码,但您可能能够摆脱以下情况:

  • 成功登录后,将用户凭据保存在会话中。
  • 首先检查会话的凭据。如果它们不存在,则检查请求参数。
于 2013-01-08T16:40:01.013 回答