Hello there,

This is just a quick question. You know that servlets respond by printing out dynamic HTML code? Well, is it possible that instead of doing that, the servlet just redirects back to the HTML form that send the servlet the Form data in the first place?

myHtml(sends form data) --------------> myServlet ---------------> myHtml


Another question I have, might as well ask. Say your servlet prints out a dynamic html page, and in that page you have a button linking to a seperate html document on your server. That button doesn't work, for some reason, you can't link to a seperate html document from a dynamically created html page by a serlvet..... Why??


Thanks guys.

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

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, 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

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.

commented: Right to the point, easy to understand explination. Well done! +1

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?

You have no idea how much you just helped me, I'm busy doing a pretty big HTML page, and it was the most excruciating job ever. 100 lines of "<html>" + "<head>" + "<title>" etc...

Otherwise, I'm using Tomcat so the request attribute data will automatically be available to the JSP pages hey?

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

commented: Thank you for helping out +16

But of course you need to have set those values in the request. Otherwise they will be null.

Correct yes.. Otherwise thank you so much! I can see some light at the end of the tunnel in this damned project again.

commented: Good luck with your project +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.