- This topic has 3 replies, 2 voices, and was last updated 19 years, 9 months ago by Riyad Kalla.
-
AuthorPosts
-
wk3000sgMember1. How do i display html codes in servlet to jsp?
2. I tink we can use PrinterWriter right?
3. if it is, how do i specify which jsp page to display and how to display?
4. And can the display be specified to where in the jsp page to display
eg. <html><body><table> (display the html codes in the servlet here) <table>…
Riyad KallaMember1) PrintWriter is a good way
2) Yes
3) You don’t, PrintWriter is a *stream* directly back to the client. There is no page involved.
4) Yes and No… I think you are confusing ideas.Something that you should note is that your JSP pages, when compiled by the application server (Tomcat, Resin, whatever) are actually turned into servlets. So Servlets and JSP pages are the same thing, they write back content to the client using the PrintWriter (let’s just say they do). If you WANT a JSP page to display something that is from your servlet, for example, a user account information, then what you can do is place the object in the request or session scope in your servlet, then use a requestDispatcher to load the JSP which can then GET the object out of the request or session and include it within itself at a certain position.
Whenever you use the PrintWriter object, whatever you write into that stream is sent back to the client’s browser, so if you want to keep communication server side when you have a servlet talking to a JSP page, then use the method I described.
I would highly suggest getting a Web Development starter book, web development in Java is far from easy to wrap your brain around if you are new to it so a good foundation is critical IMO.
wk3000sgMemberhmmm….i somehow get what u mean.
ty
Riyad KallaMemberOne more thing: Don’t think of JSP pages sitting “infront” of the Servlets on the web side… they are all equal to the app server, so it’s not like your user sends a request to the servlet, which sends it “up” to the JSP page, and then “out” to the user… instead it is more like all the servlets and JSP pages being side-by-side like this:
[Servlet] [JSP Page] [JSP Page] [Servlet]
And the Servlet or JSP page can write content back to the client using the PrintWriter… OR they can optionally store some data in a hashtable (Request, Session or Application scopes are basically hashtables) and then forward control of the client sideways to the next JSP page or Servlet… until one of the JSP pages or Servlets replies to the client.
To get a better idea, load a JSP page in your app server (assuming TOmcat), then go into the work/Catalina/localhost dir and drill down to your app’s folder, then into the org/apache/… dir until you find your JSP page’s generated Java file… open it up.
Notice how ALL your HTML code got turned into:
out.write("<html> blah blah</html>");
method calls? This might just help show what happens.
-
AuthorPosts