Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get servlet name from JSP

Status
Not open for further replies.

Diancecht

Programmer
Jan 8, 2004
4,042
ES
Hi

I'm trying to develop a web application using servlets and JSP.

Let's say that the original URL to the servlet is
The servlets processes the request and sends it to the jsp. When the request reaches the jsp, it's something like
I want to include a link on the page, using JSP, that points to the original url, but with secure protocol, that is and my problem is that I can't access the servlet name from the jsp.


Now my question is: does anyone know a way to access the servlet name from the jps?

Thanks in advance.

Dian
 
Thank you for your reply, sedj, but I've already configured the server and everything is running. My problem is recovering the servlet name after the jsp has been invoked.

Cheers.

Dian
 
Sorry, wrong end of the stick !

You can get the sending, or referring URL using

request.getHeader("referer")
 
Thank you again for your reply sedj, but that header seems to be empty on my request.

I think I will end doing some client-side scripting to solve this problem :)

Cheers.

Dian
 
Hmmm... if you have a <form>, or <a href> that submits/goes to aother page, the &quot;referer&quot; header seems to be populated, but if you inclde() or forward() from a servlet, its not there.

I guess you could always populate a HttpSession attribute ...
 
I guess my bad luck is having a nice day. The &quot;referer&quot; header seems to be lost in space, either in the original request and in the forwarded one.

Thanks for the httpSession sugestion, but this process is involved inside a big, general purpose architecture that cannot be changed so happily. So if I can't find a way to get that servlet name, I guess the client side will be my last chance.

Cheers.

Dian
 
OK, I've got a decent URL coming from a servlet - interestingly though, the behaviur is different to hitting it from the test.jsp.

Anyway, below are the three test components for you to check out :

Code:
public class TestServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = getServletContext().getRequestDispatcher(&quot;/test1.jsp&quot;);
		rd.forward(request, response);

    }
}


----------------------------------------------------------
<!-- test.jsp -->
<html>
<body>

	<form action=&quot;test1.jsp&quot; method=&quot;post&quot;>
  		<input type=&quot;submit&quot;>
	</form>
</body>
</html>

----------------------------------------------------------

<!-- test1.jsp -->
<html>
<body>

<%


out.println(&quot;REF :&quot; +request.getHeader(&quot;referer&quot;) +&quot;<br>&quot;);
out.println(&quot;URL :&quot; +request.getRequestURL());

%>

</body>
</html>

 
Thanks again, sedj.

The pieces of code you posted are actually similar to the ones I'm using.

The problem with the requestURL method is that the return value is not the same in the servlet or in the jsp. When invoked in the servlet, it returns but the same code, when executed from the jsp, returns since the target URL has changed, and the former value is not accesible.

Furthermote, the getHeader method returns null.

Maybe what I'm trying is not possible.

Cheers.

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top