It's not a secret anymore!*
The moose likes JSP and the fly likes Including Servlets to JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Including Servlets to JSP page " Watch "Including Servlets to JSP page " New topic
Author

Including Servlets to JSP page

Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Hi all,
What is the best way to include a servlet and it's helper classes to a JSP page.
i need session values from JSP pages to servlets and vise versa.
RequestDispatcher.include() will work?..if soplz tell me how to make it happen.
anyone experienced plz share
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
You can include any type of page i.e. html,jsp in JSP using <jsp:include page="/somepage.jsp"/> tag.
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Originally posted by Ali Gohar:
You can include any type of page i.e. html,jsp in JSP using <jsp:include page="/somepage.jsp"/> tag.

but i need session variables from previous page to servlet page.is this method
work for that?.
thanks for ur reply
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Yes session object will be available in that page too.
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
thanks ..i m trying that..
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193

getting this error ..any idea on this error?
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
where are you getting this error? and in which line of code the problem is? If you don't know then send the piece of code you added
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193

this is code.if i remove include page,no errors,so it's a problom with that line or the placement of that line.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
The problem is in the following line of code
<%@include page ="/myservlet/login"%>
the attribute is "file" not "page".
Make it <%@include file ="/myservlet/login"%>
john von
Ranch Hand

Joined: Apr 13, 2004
Posts: 49
How about replacing the URL with a variable?
Say:
<%! String url = "/index.html" %>
...
...
<%@ include file = url %>
How would i do it?
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Originally posted by Ali Gohar:
The problem is in the following line of code
<%@include page ="/myservlet/login"%>
the attribute is "file" not "page".
Make it <%@include file ="/myservlet/login"%>

i have changed it to,
<%@include file ="http://localhost:8080/myservlet/login"%>
but now the error is

if i type http://localhost:8080/myservlet/login in brawser,it works cool.but in within JSP it returns error.where's the trick?.
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
my servlets are packaged in anothee doirectory in WEBAPPS than my JSP directory.servlets are a bundle and it's own context settings.JSP pages have another context settings .but both runs in same server and same DB server with same driver and username and password.
if i type URL of my JSP page and Servlet page in a brawer,it comes cool,but using servlet within JSP brings error.what maybe the cause?.anyone experienced?
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Why are you including a servlet in jsp? why don't you include jsp instead of servlet?
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Originally posted by Ali Gohar:
Why are you including a servlet in jsp? why don't you include jsp instead of servlet?

servlets are prewritten code.i just wanna use them in my present work.it might be easy.but i donno that key.
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Originally posted by Murasoli Maran:

servlets are prewritten code.i just wanna use them in my present work.it might be easy.but i donno that key.

how to make my servlet to JSP without hacking the servlet code?.making my servlet to a new JSP page is also a solution.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
It would not take much time to convert your servlet to jsp. Try to do that it will be easier to include jsp.
just copy and paste the code from servlet doGet or doPost method in JSP Scriplets.
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Originally posted by Ali Gohar:
It would not take much time to convert your servlet to jsp. Try to do that it will be easier to include jsp.
just copy and paste the code from servlet doGet or doPost method in JSP Scriplets.

then what abt context settings?.anyhow i'll try to get that in..
nice to talk with u Ali,keep it up
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
i have done all the summersault.but no way to solve.
but the problom seems very simple.just adding a servlet to a JSP page with session variables.
my problom is in my servlet bundle,a context setting is there as well as JSP bundle also having context settings.
but i just need to call that servlet page just like another HTML page.javascript may do it.but if i do with javascript,i dont get session varibles.
do i hav to hack all servlet code to make to include in my JSP app??.
i think there should be a method for including servlet into JSP.Ali told
<%@include file ="http://localhost:8080/myservlet/login"%> method.but that's not working.
i have tried RequestDispatcher.include method also ,that's also not working becoz of a .properties file missing problom.
i think it's a silly problom.anyone experienced,plz share thoughts
thx in adv
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Ali told
<%@include file ="http://localhost:8080/myservlet/login"%> method.but that's not working.

why are you using "http://localhost:8080/myservlet/login" to include the servlet? Just use <jsp:include page="/myservlet/login"/>. Try to do this.
Murasoli Maran
Ranch Hand

Joined: Jun 08, 2003
Posts: 193
Originally posted by Ali Gohar:

why are you using "http://localhost:8080/myservlet/login" to include the servlet? Just use <jsp:include page="/myservlet/login"/>. Try to do this.

there is the problom.
servlet bundle is different from JSP folder.servlets hav it's own folder and JSP hav it's own folder also in webapps directory.and each having it's own context settings also.
so jsp:include page="/myservlet/login"/ will not work for my case.
anyhow i m doing someother part in my app.hope soon i return to this.i defenetely need a simple solution to include the servlets than hacking the servlet code.coz it's not done by me and every settings are different than my app including DB Driver and calling lots of resources with many path settings.i can hack the code.but it'll cost lots of my time.still i believe i'll find a good and easy method to avoid unnecessory reformating of code.i wanna reuse simply.i like pluggin model.why cant JSP pluggin the servlets?
plz share thoughts
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Including Servlets to JSP page