0

I have an index.jsp page with buttons named "refresh", "forward" and "edit". If I click on the refresh button then it call a Servlet and values are displayed on the index.jsp page.

When I click on the "forward" button it calls another servlet and goes to another page forward_call_log.jsp. In this page when I click on the "forward" button it calls another servlet and that servlet displays the index.jsp page. The index.jsp page is displayed but the values from the data base are not available.

How can i fix this problem?

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>

After click on the Refresh button call 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)
        {

        }
    }

}

}

After clicking on the Forward button call 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();
        }

}

 }
4

1 に答える 1

0

リクエストオブジェクトにデータを添付していないようです。そのため、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デザインパターンについて読むことができます。

于 2012-06-08T08:29:09.113 に答える