This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes how do i include servlet in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how do i include servlet in jsp" Watch "how do i include servlet in jsp" New topic

how do i include servlet in jsp

roopa sudhi

Joined: Oct 31, 2001
Posts: 16
hi all,
I need to include a servlet[a basic authentication servlet which will assign a session for the authenticated user]
in my JSP page to check whether the person accessing that page is authenticated.
I tried to use include but i am getting error saying the servlet was not found im sure the path is correct.
I tried to use <jsp:include page="" flush="true">
this doesnt throw any error but it is not authenticating the person.
I appreciate any help.
Manikandan Pattathil

Joined: Feb 13, 2002
Posts: 2
Usually the scenario explained will be handled like this. You dont have to include a JSP in Servlet.
1. Validate the login and authenticate the user.
2. If logged in forward to Success.jsp
3. Else forward to Failure.jsp
You can use the sendRedirect() method in HTTPServletResponse class to navigate to the Success.jsp or Failure.jsp depending on the condition.
See below from API method help.
public abstract void sendRedirect(String location) throws IOException
Sends a temporary redirect response to the client using the specified redirect location URL. The URL must be absolute (for example, https://hostname/path/file.html). Relative URLs are not permitted here.
location - the redirect location URL
Throws: IOException
If an I/O error has occurred.
john wu

Joined: Nov 27, 2001
Posts: 7
You can not include a servlet, but you can use a servlet in your jsp.
there are three options for using a servlet in jsp.
1. import your servlet class, and create a instance of servlet in your jsp.
2. make your servlet as a bean of your jsp.
3. write your own tag library.
hope these three cents are helpful.
tatae alvostru

Joined: Jun 04, 2001
Posts: 17
Use RequestDispatcher
rd = getServletContext().getRequestDispatcher("file to include - jsp or servlet");
roopa sudhi

Joined: Oct 31, 2001
Posts: 16
Thanks for ur replies,
I used forward,and a jsp to store the original url from where it that it can come back to the same page.
thanks again.
I agree. Here's the link:
subject: how do i include servlet in jsp
It's not a secret anymore!