0

I am trying to build a site that displays report results based on a date range inputted by the user. First I built the front-end "aesthetics" part of it in HTML/CSS/JS using dummy json data to retrieve the results. Now I am trying to eliminate the json and actually integrate it with the backend, which I have no experience with so am struggling a bit.

Right now I have a Java servlet written, and it works alright. I'm writing the ResultSet from the database query by doing response.getWriter().write(""); Now from what I've seen the next step is to display the results by appending to the URL, but the code that formats and displays it properly is just a div currently. What is the proper way to do this/modify the code?

Sorry if this is a badly written question, I'm not totally sure of all the terminology or best practices, although I'm trying to learn.

EDIT: Currently on the html page I have

$('#gen-report').click(function(){
     $("#auction-report").fadeIn({ duration: 400 });
}

In the auction-report div is all of my formatting & elements (charts, displays, etc.) Is there a way to make use of this code instead of having to start my jsp page from scratch?

newtothis
  • 71
  • 8

1 Answers1

1

Put parameters in servlet to request, then invoke forward from request.getRequestDispatcher.

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setAttribute("now", LocalDate.now());
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/test.jsp");
            dispatcher.forward(request, response);
    }

Now you have access to params which you put in request. Here jsp:

<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Test</title>
</head>
<body>
    <h2>Date</h2>

    <%= request.getParameter("now") %>
</body>
</html>
Maxim Kasyanov
  • 938
  • 5
  • 14
  • Thanks, i appreciate this! But my question was more about how if I know what I want to display already -- I'll edit the question to add my code – newtothis Jul 12 '18 at 17:32
  • @newtothis Okey, Let's clarify the question. You need return json from servlet to jsp, Is it? I don't understand nothing from the question – Maxim Kasyanov Jul 12 '18 at 17:38
  • My apologies, it wasn't written that well. I don't have a jsp page, I have an html page (but from what I understand they're similar?). Otherwise yes. And I have the formatting for the data done already (such as counting the # of results and displaying it, putting it in a pie chart, etc.) but I don't know how to integrate the formatting with the data I get from the servlet. – newtothis Jul 12 '18 at 17:45
  • @newtothis Okey, a bit more clearly. I need to send ajax request to the servlet. I servlet process the request and return json. In html put data from response to pie chart graph. This link should help to you https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax/ – Maxim Kasyanov Jul 12 '18 at 17:50