hi i wrote a question a weeks ago about servlets and JSP

I thank to jimbal2 for help me on the topic "how to comunicate from servlet to a JSP and viceversa?", the solution was:

You have a mechanism called "forwarding", with which you can pass control from one resource (servlet/jsp) to another resource (servlet/jsp). You do this using a RequestDispatcher, which you can get from the HttpServletRequest object. If for example you want to forward to "index.jsp" which is in the root of your web application, you would do this in your servlet:

RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");

rd.forward(request, response);

return;

(watch the return statement, you must manually return after a forward, or else the servlet will continue executing!). A forward will NOT create a new request, the same request is passed to the new resource (to create a new request, use a redirect in stead, also done with the RequestDispatcher).

To pass objects between the two resources when doing a forward, use request.setAttribute(), request.getAttribute() and jsp:useBean.

-

but i have some other questions, and these are :

so i've to do this with sessions and everything i want to pass to: for example my application is a servlet based, from a servlet to a JSP so i have to do it with:

RequestDispatcher rd = request.getRequestDispatcher("/servletToJsp.jsp");

rd.forward(request, response);

return;

if its from a JSP to a servlet it is the same way?, if i want to pass the session from one to another and viceversa how do i do that?,and its possible to import into a JSP a class that isn't a bean? because like i wrote here my appplication is a servlet-based but i want to insert new JSPs so thanks for your help and if its possible to respond my questions.

[1792 byte] By [omm79a] at [2007-11-27 1:40:50]
# 1

>but i have some other questions, and these are :

>so i've to do this with sessions and everything i want to pass to: for example

>my application is a servlet based, from a servlet to a JSP so i have to do it with:

>RequestDispatcher rd = request.getRequestDispatcher("/servletToJsp.jsp");

>rd.forward(request, response);

>return;

>if its from a JSP to a servlet it is the same way?,

JSP is actually generated as a HTML.

You can simply use a HTML form to submit data to a Servlet

>if i want to pass the session from one to another and viceversa how do i do that?,

http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpSession.html

session.setAttribute("id",123);

session.getAttribute("id");

Similar to request.setAttribute() but last for a session.

>and its possible to import into a JSP a class that isn't a bean? because like

> i wrote here my appplication is a servlet-based but i want to insert new JSPs

http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html#JSPIntro3

You can import java.util.*, java.io.* , etc.

>so thanks for your help and if its possible to respond my questions.

See if it helps

rym82a at 2007-7-12 0:55:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...