Actually, you never create pages nor write HTML code in servlets. You may be able to do that, but you don't.
You use servlets for handing data, doing operation, reading from database and then you send the results to the jsp page. It is not a good idea to write html code in servlets. J
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Actually, you never create pages nor write HTML code in servlets. You may be able to do that, but you don't.
You use servlets for handling data, doing operation, reading from database and then you send the results to the jsp page. It is not a good idea to write html code in servlets. After you have sent the data at the servlet (like you do), do whatever operations you like and then redirect to a JSP page with the results for displaying or for whatever you want.
You might want to look at the class RequestDispatcer. In the servlet, after you are done with whatever you are doing, you can do this:
RequestDispatcher dispatcher = request.getRequestDispatcher("myHtml.jsp");
request.setAttribute("attr1", "Value");
request.setAttribute("attr2", new Integer(1));
dispatcher.forward(request, response);
And in the myHtml.jsp you can do this:
String s = (String)request.getAttribute("attr1");
Integer iObj = (Integer)request.getAttribute("attr2");
You need to check if the s, iObj are null or not, before you use them. If you go to that page without going through that servlet you will need to ignore them
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
So what you saying. Is that instead of having a single servlet generate 3 "html pages".. Rather have 3 servlets that obtain data from the database etc, and 3 JSPs that then uses those 3 servlets repectively?
Actually I had a problem with the browser reloading that is why my first post didn't make much sense. Read my latest post.
Also
You can do whatever you want based on your needs.
Example:
After reading your last comment, you can have 1 servlet that redirects to whatever html you want:
String url = "";
if (....) {
url = "somepage.jsp"
request.setAttribute("att1", "value1")
} else if (....) {
url = "somepage2.jsp"
request.setAttribute("att2", "value2")
}
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
You don't need to have 1 servlet for all pages. You can have 1 servlet that goes to 1 jsp page,
Or 1 servlet with if statemets that goes to many pages.
Many similar pages that have some sort of common operations so they need to go the one servlet that has the code you want to execute and redirect to wherever you want.
BUT. I general: HTML code in servlets: bad idea. I am sure that you already know that; after trying to write html code like this:
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>Hello world</p>");
out.println("</body></html>");
out.close();
Imagine wanting to write a huge jsp page with many tags and complex javascript code. Even the above is difficult to understand, write and correct.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Wow, nice.. That's pretty cool. Makes the code in the files less and much more manageable. In the myHtml.jsp, you don't need to import anything like that? You literally just do this:
<%
String s = (String)request.getAttribute("attr1");
Integer iObj = (Integer)request.getAttribute("attr2");
%>
And the request/attribute data will be "reachable" for the jsp page?
Yes,
But of course you need to have set those values in the request. Otherwise they will be null.
That is happening when you do this: dispatcher.forward(request, response);
You set the attributes at the request instance and send that request to the jsp page of the dispatcher
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448