1,105,225 Community Members

Servlet Response Type

Member Avatar
Dean_Grobler
Posting Whiz in Training
221 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

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

Member Avatar
Dean_Grobler
Posting Whiz in Training
221 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

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

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
1
 

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.

Member Avatar
Dean_Grobler
Posting Whiz in Training
221 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Dean_Grobler
Posting Whiz in Training
221 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
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?

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

Member Avatar
Dean_Grobler
Posting Whiz in Training
221 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: