我有一个index.jsp页面,其中包含名为“refresh”,“forward”和“edit”的按钮。如果我单击刷新按钮,则它会调用Servlet,并在index.jsp页面上显示值。
当我点击“转发”按钮时,它会调用另一个servlet并转到另一个页面forward_call_log.jsp。在此页面中,当我单击“forward”按钮时,它会调用另一个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>
点击刷新按钮后,调用 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)
{
}
}
}
}
点击转发按钮后,调用 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();
}
}
}
答案 0 :(得分: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 Design Pattern以了解更好的代码设计方法。