Exit Servlet in JSP

I am passing parameters from the calendar.jsp JSP page to servlet connect.java. In the Java class, I pass the request from the database. It retrieves the data in order, but I need the result to be returned to the JSP page.

I tried the following code, but with an error in out.println() .

 @WebServlet("/calendar") public class Connect extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fromDate = request.getParameter("fromDate"); String toDate = request.getParameter("toDate"); System.out.println("fromDate---->"+fromDate); System.out.println("toDate---->"+toDate); String query = "SELECT action_time,user_action,user_ip,user_id FROM ksdi.login_detail where (action_time, action_time) OVERLAPS (DATE '" + fromDate+ "',DATE '"+ toDate+ "'+ integer '1')" + " order by action_time desc"; Connection conn = null; PreparedStatement statement = null; ResultSet resultSet = null; try { conn = ConnectionUtil.getConnection(); statement = conn.prepareStatement(query); resultSet = statement.executeQuery(); if (resultSet.next()) { while (resultSet.next()) { String action_time=resultSet.getString("action_time"); String user_action=resultSet.getString("user_action"); String user_ip=resultSet.getString("user_ip"); String user_id=resultSet.getString("user_id"); System.out.println((action_time) + " " + (user_action) + " "+(user_ip) + " "+(user_id) + " "); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("action time"+((ResultSet) request).getString("action_time")+"<br />"); RequestDispatcher view = request.getRequestDispatcher("calendar.jsp"); view.forward(request,response); } } else { System.out.println("not found"); } } catch (SQLException e) { throw new ServletException("DB interaction failed", e); } catch (Exception e) { e.printStackTrace(); } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {} if (statement != null) try { statement.close(); } catch (SQLException ignore) {} if (conn != null) try { conn.close(); } catch (SQLException ignore) {} } } } 


Here is my JSP file:

 <%@ page import="java.sql.*" %> <% String path = request.getContextPath();%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script src="<%=path%>/js/calendar.js"></script> <link href="<%=path %>/css/calendar.css" rel="stylesheet"> </head> <body> <form action="calendar"> <div class="container"> From:<input name="fromDate" type="text" class="calendarSelectDate" /> To: <input name="toDate" type="text" class="calendarSelectDate" /> <input type="submit" name="b1" value="Go"> </div> <div id="calendarDiv"></div> </form> </body> </html> 
+4
source share
3 answers

1) In your servlet code, you are doing it wrong. You must either output using PrintWriter println() or use the RequestDispatcher forward() method. But NOT like in the same servlet method .

Quote: What is a query manager and how to use it?

Unlike the case with 'include', 'forward' discards the previous output that the servlet wrote back .

See examples on the above page.

And if you decide to use forward() to transfer data previously received from your database, you can save this data in the request area, i.e. set request attributes e.g.

 request.setAttribute("actionTime", action_time); 

Of course, you must do this before calling forward() !

Then in your JSP you can display this data using Expression Languge , for example:
test.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Test Page</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h1>Testing the passed values</h1> <p>Action time: ${actionTime}</p> <!-- same way for other data --> </body> </html> 


2) Another problem in your servlet code is a line that has the following:
((ResultSet) request).getString("action_time")

Neaither ServletRequest and HttpServletRequest has a getString() method. Although the ResultSet has such a method, it is pointless and not in your case to send a request to the ResultSet.


PS
Using scripts in JSP is not recommended

+3
source

Can the returned HttpServletRequest request return in a ResultSet , I think that it should use the ResultSet getString method without casting.

  PrintWriter out = response.getWriter(); out.println("action time"+ resultSet.getString("action_time") + "<br />"); 
0
source

Well set the values ​​you get in httprequest like this in servlet

PS: I commented on unnecessary lines

  //response.setContentType("text/html"); //PrintWriter out = response.getWriter(); // out.println("action time"+((ResultSet) request).getString("action_time")+"<br />"); request.setAttribute("action_time",action_time); request.setAttribute("user_action",user_action); request.setAttribute("user_ip",user_ip); request.setAttribute("user_id",user_id); RequestDispatcher view = request.getRequestDispatcher("calendar.jsp"); view.forward(request,response); 

And in the calendar.jsp you are sending to, refer to the values

 <%= request.getAttribute("action_time")%> <%= request.getAttribute("user_action")%> <%= request.getAttribute("user_ip")%> <%= request.getAttribute("user_id")%> 
0
source

All Articles