【问题标题】:JSP not displaying data from POST request to ServletJSP 不显示从 POST 请求到 Servlet 的数据
【发布时间】:2012-06-08 07:26:50
【问题描述】:

我有一个 index.jsp 页面,其中包含名为“刷新”、“转发”和“编辑”的按钮。如果我单击刷新按钮,那么它会调用一个 Servlet,并且值会显示在 index.jsp 页面上。

当我单击“转发”按钮时,它会调用另一个 servlet 并转到另一个页面 forward_call_log.jsp。在此页面中,当我单击“转发”按钮时,它会调用另一个 servlet,并且该 servlet 会显示 index.jsp 页面。显示 index.jsp 页面,但数据库中的值不可用。

我该如何解决这个问题?

index.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pager Example - www.javaworkspace.com</title>
</head>
<body>
<form method="post" action="./Problem_retrive" >
<input type="submit" value="Refresh">
</form>
<form action="./edit_call_log" method="post">
<%
ResultSet rs=null;
try
{
    rs=(ResultSet)request.getAttribute("rs");
    //request.setAttribute("rs",rs);
    if(rs.next())
    {
    %>
    <table border=1 cellspacing=1 cellpadding=1>
        <tr>
            <th>check box</th>
            <th>Problem ID</th>
            <th>user ID</th>
        </tr>
        <%
            do
            {
        %>
                <tr>
                    <td><input type="checkbox" name="checkbox"   value="<%=rs.getString(1) %>"></td>
                    <td><%=rs.getString(1) %></td>
                    <td><%=rs.getString(2) %></td>
                </tr>
        <%
            }
            while(rs.next());
        %>
    </table>
<%
    }
    else
    {
        out.println("hiiiiiiii");
    }
}
catch(Exception e)
{
    e.printStackTrace();
}
 %>

<input type="submit" name="act" value="EditCall"/>
<input type="submit" name="act" value="Forward"/>   
</form>
</body>
</html>

点击Refresh按钮后调用Edit_call_servlet.java

import database.problemdesc.Problem_retrive_class;
import database.user_master.User_master;
import java.sql.*;
public class Edit_call extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException 
{
    response.setContentType("text/html");
    HttpSession session = request.getSession(true);
    ResultSet rs=null;
    Problem_retrive_class prc=new Problem_retrive_class();
    int checkbox=Integer.parseInt(request.getParameter("checkbox"));
    session.setAttribute("problem_id", checkbox);
    String act=request.getParameter("act");
    if(act.equals("EditCall"))
    {

        try
        {


            rs=prc.select_table(checkbox);
            RequestDispatcher rd =   request.getRequestDispatcher("edit_call_log.jsp");
            request.setAttribute("rs", rs);
            rd.forward(request, response);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    else if(act.equals("Forward"))
    {
        System.out.println("Forward page");
        try
        {
            ResultSet rs1=null;
            User_master um=new User_master();
            rs=prc.select_table(checkbox);
            if(rs.next())
            {
                System.out.println("Value retrieve from PRC select_table");
                rs1=um.select_table(rs.getString(7));
                System.out.println("Value retrieve from PRC   select_table"+rs.getString(7));
                RequestDispatcher rd = request.getRequestDispatcher("forward_call_log.jsp");
                request.setAttribute("rs1", rs1);
                request.setAttribute("user_type", rs.getString(7));
                rd.forward(request, response);
            }
            else
            {
                RequestDispatcher rd =  request.getRequestDispatcher("index.jsp");

                rd.forward(request, response);
            }

        }
        catch(Exception e)
        {

        }
    }

}

}

点击Forward按钮后调用Problem_retrive_servlet.java

import database.problemdesc.Problem_retrive_class;


public class Problem_retrive extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    Connection con=null;
    ResultSet rs=null;
        try
        {
            Problem_retrive_class prc=new Problem_retrive_class();
            DBBean db=new DBBean();
            con=db.getDBConnection();
            rs=prc.select_table();

            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");

            rd.forward(request, response);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

}

 }

【问题讨论】:

  • 您描述的问题是forward_call_log.jsp 上的一个名为Forward 的按钮,但是您没有包含此代码。请定义单击该按钮时将触发哪个类和方法?

标签: java html jsp servlets


【解决方案1】:

看起来您没有将任何数据附加到请求对象,因此 index.jsp 没有可显示的内容。我假设这是发生问题时正在执行的代码。

public class Edit_call extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    ...

    else if(act.equals("Forward"))
    {
        try
        {
            ...
            {
                ...
            }
            else
            {
                RequestDispatcher rd =  request.getRequestDispatcher("index.jsp");

                / *** YOUR PROBLEM IS HERE ***/

                rd.forward(request, response);
            }

[编辑]

根据您的问题,我假设您的 JSP 上的“刷新”按钮按预期工作(即数据库中的数据按预期显示)。从下面的JSP代码...

<form method="post" action="./Problem_retrive" >
    <input type="submit" value="Refresh">
</form>

服务器上正在执行的方法是Problem_retrive.doPost()

基于此,我们需要了解在forward_call_log.jsp 上单击“前进”按钮时正在执行的代码。您已经描述了这个“前进”按钮显示index.jsp,但不包括数据库中的数据。假设它调用Edit_call.doPost()*。

*我建议您在所有 doPost() 方法中放置调试器断点以确认此假设,或者如果您无法调试,请参考您的 System.out.println() 语句。

因此,当没有显示数据时,您需要将 Problem_retrive.doPost() 中发生的情况与 Edit_call.doPost() 中未发生的情况进行比较。我怀疑你没有打电话给request.setAttribute()

rs=prc.select_table();
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
request.setAttribute("rs", rs);
rd.forward(request, response);

关于优秀设计的旁注

您可以阅读MVC Design Pattern 以更好地设计代码。

【讨论】:

  • @Prasanta。作为旁注,最好不要将 JDBC 相关对象传递给您的 JSP(即ResultSet)。您的控制器类应该从结果集中提取它需要的值并创建某种集合(例如ArrayList)。
  • 你能用一个例子解释一下吗.....因为我是jsp-servlet的新手......这对我很有帮助............
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多