从servlet将问题重定向到JSP

时间:2015-07-10 12:05:31

标签: jsp servlets httprequest

我有一个servlet S1,它将从我的数据库中获取数据并将其存储在数组列表“A”中。这个数组列表A将被发送到一个JSP,我将在其中打印数组列表A的所有内容。我能够打印所有细节。现在在JSP中我有一个单选按钮,我将它传递给其他servlet S2并从DB获取详细信息并返回到调用servlet S2的JSP。现在servlet S2的所有进程都正常工作,但我无法回到JSP。我在数组列表A的print语句中得到一个jasper异常。我认为数组列表A的值不可用,因为它会丢失范围,从而导致错误。代码如下。

import math as m 

def findfac(n):
    faclist = [1]
    for i in range(2, int(m.sqrt(n) + 2)):
        if n%i == 0:
            if i not in faclist:
                faclist.append(i)
                if n/i not in faclist:
                    faclist.append(n/i)
    return facts
  

JSP

//**Servlet S1**
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class EditUsers
*/
@WebServlet("/EditUsers")
public class EditUsers extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
 * @see HttpServlet#HttpServlet()
 */
public EditUsers() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("entered post");
    Connection con=null;
    PreparedStatement ps= null;
    ResultSet rs= null;
    int i=0;
    ArrayList<User> user= new ArrayList<User>();
    try
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    //  System.out.println("connection");
        con =DriverManager.getConnection("url","user", "pass");
        ps = con.prepareStatement("select * from user_Dtls");
        rs = ps.executeQuery();
        while(rs.next())
        {
        //  System.out.println("Entered While");
            User u= new User();
            u.setUserId(rs.getString(1));
            u.setUser(rs.getString(2));
            u.setRole(rs.getString(3));
            u.setStatus(rs.getString(4));
            user.add(u);
        }
            request.setAttribute("ArrayOfDetails", user);
            RequestDispatcher rd= request.getRequestDispatcher("EditUserDetails.jsp");
            rd.forward(request, response);

    }

    catch(Exception e)
    {
        System.out.println(e);
    }

    finally
    {
        if(con!=null)
        {
            try 
            {
                con.close();
                rs.close();
                ps.close();
            } 
            catch (SQLException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    }
}

}

  

Servlet S2

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.lang.*, java.util.*, com.User"%>
<!DOCTYPE html">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<meta charset="utf-8">
  
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script type="text/javascript">
  function display()
  {
	  document.getElementById('1').style.display='block';
	  document.getElementById('2').style.display= 'block';
	  document.getElementById('3').style.display= 'block';
	  document.getElementById('4').style.display='block';
	  document.getElementById('5').style.display='block';
	  document.getElementById('6').style.display='block';
	  document.getElementById('7').style.display='block';
	  document.getElementById('8').style.display='block';
	  document.getElementById('9').style.display= 'block';
}
  </script>
 <body>
<form role="form" method="post" action = "SearchUsers">
<%
ArrayList<User> userList= (ArrayList<User>) request.getAttribute("ArrayOfDetails");
%>

<table  class="table">
	<tr>
	<th>USER ID:</th>
	<th>USER NAME:</th>
	<th>USER ROLE:</th>
	<th>USER STATUS:</th>
	</tr>
	<% for(User user : userList)                //for printing
	{
	%>
	<tr>
		<td><input type="radio" name="userId" value="<%=user.getUserId() %>"/><

%=user.getUserId() %></td>
		<td><%=user.getUser() %></td>
		<td><%=user.getRole() %></td>
		<td><%=user.getStatus() %></td>
	</tr>
	
	<%
	}
	%>
</table>
<input type="submit" name="useredit" value="Edit" onclick= "display();"/>
<%ArrayList<User> arrU = new ArrayList<User>();                   
arrU = (ArrayList<User>) request.getAttribute("uEdit");             //retriving data from Servlet 2
%>
<style = "display:none" id = "1"> Name: </style> <input type="text" id= "2" name="userName" hidden 

value="<%=arrU.getUser%>"></input> <br>

<style = "display:none" id = "3">Role: </style> <select  id="4" name="Roles" style = "display:none" 

value="<%=arrU.getRole%>">
        				<option value="Admin">Admin</option>
				        <option value="UnderWriter">UnderWriter</option>
				        <option value="Manager">Manager</option>
				   		</select><br>
				   		
<style = "display:none" id = "5">Status: </style> <select  id = "6" name="Status" style = 

"display:none" value="<%=arrU.getStatus%>">
        				<option value="Active">Active</option>
				        <option value="Locked">Locked</option>
				        <option value="Disabled">Disabled</option>
				   		</select> <br>
<input type="submit" id = '7'style ="display:none" name="userupdate" value="Update the details"/>

</form>
</body>
</html>

我使用了正确的网址,用户,传入我的程序。我在这里把它作为假的。在

获取jasper异常
  

for(User user:userList)

当我从Servlet 2返回JSP时 请不要介意低级编程风格。 servlet / jsp编程新手。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你说你正在重定向,但实际上你并没有重定向回来。你正在转发。

将此转发替换为第二个servlet中第一个servlet的JSP,

response.sendRedirect(request.getContextPath() + "/EditUsers");

通过真正的重定向到第一个servlet本身(反过来将调度所需的JSP):

 - (void) showActivityIndicator{

    self.spinner.hidden=NO;


    [self.spinner startAnimating];

    [self.view bringSubviewToFront:self.spinner];

    [self.view setNeedsDisplay];


    }

另见: