我正在尝试调用一个Servlet,该Servlet以json格式从另一个Servlet写入从数据库中获取的javaobject。
我的代码流是Servlet1 check_login
-> Servlet2 jsonCreate
尝试执行此操作时出现HTTP 404错误。
这是我的check_login.java Servlet代码
@WebServlet("/Check_login")
public class Check_login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user_name=request.getParameter("user_name");
String password=request.getParameter("password");
try {
String role=check_database(user_name,password);
if(role.equals("")) {
response.sendRedirect("index.html");
}else if(role.equals("admin")) {
List<Programs> Programs_Offered = new ArrayList<Programs>();
RequestDispatcher rd = request.getRequestDispatcher("jsonCreate");
rd.forward(request,response);
}else if(role.equals("mac")) {
response.sendRedirect("mac_welcome.jsp");
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
并且,这是jsonCreate.java Servlet代码
@WebServlet("/jsonCreates.json")
public class jsonCreate extends HttpServlet {
public static List<Programs> list() throws SQLException, IOException {
List<Programs> Programs_Offered = new ArrayList<Programs>();
Connection conn=DataBase_Connection.getInstance().getConnection();
Statement ps=conn.createStatement();
ResultSet rs = ps.executeQuery(Queries.view_programs);
while(rs.next()){
Programs p=new Programs();
p.setProgramName(rs.getString("ProgramName"));
p.setDescription(rs.getString("Description"));
p.setApplication_Eligibility(rs.getString("Applicant_Eligibility"));
p.setDuration(rs.getInt("Duration"));
p.setDegree_Certificate_Offered(rs.getString("Degree_Certificate_Offered"));
Programs_Offered.add(p);
}
return Programs_Offered;
}
private static final long serialVersionUID = 1L;
public jsonCreate() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Programs> categories=null;
try {
categories = jsonCreate.list();
} catch (SQLException e) {
e.printStackTrace();
}
String categoriesJson = new Gson().toJson(categories);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(categoriesJson);
//response.sendRedirect("admin_welcome.jsp");
}
当我使jsonCreates.json的名称与Java servlet名称(jsonCreate)相同时,它运行良好,并在URL
http://localhost:8081/servlet_demo/jsonCreate
的页面上打开json数据。再一次,当我重定向到新的JSP
admin_welcome.jsp
时,它打开没有任何问题,但是在链接http://localhost:8081/servlet_demo/jsonCreate
中找不到任何json数据。我犯了一些错误,但找不到。有人可以告诉我这是什么。
最佳答案
您应该将jsonCreates.json的名称设置为与Java servlet名称(jsonCreate)相同的名称
@WebServlet("/jsonCreate")
之所以不获取json数据,是因为response.sendRedirect()不转发任何数据,它只是将重定向(导航)到页面。
如果您希望在整个页面导航中都可以访问JSON数据,则应创建一个会话并设置一个会话变量以保存此JSON数据。
爪哇
HttpSession session = request.getSession(false);
session.setAttribute("variable", "json value");
response.sendRedirect("/page");
JSP
<%
out.println(session.getAttribute("variable"));
%>
要么
您可以使用forward()如下:
爪哇
request.setAttribute("variable", "JSON data");
RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
JSP
<%
out.println(request.getAttribute("variable"));
%>