Concepts for page path in JSP and servlets - java

What is the difference between these two?
In JSP:
${pageContext.request.contextPath}
and how to achieve same results if coded without Expression Language and in a servlet?
In servlet:
request.getServletPath()
and how to achieve same results if coded in JSP?

${pageContext.request.contextPath} : Returns the portion of the request URI that indicates the context of the request. In fact, it is identical to request.getContextPath(), since ${pageContext.request} refers to the HttpServletRequest of the current request.
For example:
http://localhost:80/myapplication/path/servlet
${pageContext.request.contextPath} returns /myapplication
request.getServletPath() Returns the part of this request's URL that calls the servlet, e.g. /path/servlet
${pageContext.request.servletPath} returns /path/servlet

As an answer to the second part of your question:
request.getServletPath()and how to achieve same results if coded in
a JSP using Expression Language?
Here the equivalent of request.getServletPath()using Expression Language:
${pageContext.request.servletPath}
If you want to use other ServletRequest/HttpServletRequest methods by means of the Expression Language, just use ${pageContext.request.method}, where method is one of the appropriate methods.

In the case of a view handled by a JSP, then ${pageContext.request.servletPath} returned the path of the jsp, and not the servlet path from the web.xml.
To get that I used: ${requestScope['javax.servlet.forward.servlet_path']}

Related

Accessing the value of servlet in two jsps

I have a Servlet, which sends values to a JSP1. I am setting the values in the servlet as
request.setAttribute();
and i am using
dispatcher.forward(request, response);
and i am able to access these values in the JSP1 using expression language.
Now, I also need to use this in another JSP2.
These two JSP's are clubbed together to display in a single page. So, I need to access the values of the servlet in the second jsp as well. How can i do that?
As you say you have both the jsp clubbed in a single page , you can simply get the attribute as,
${attributeName}
it refers to the object set in the HttpServletRequest scope . I suggest to take the tutorial for Expression language
It is quite simple. If you are setting an attribute "attr1" in a servlet and then forwarding to a JSP (file1.jsp):
request.setAttribute("attr1", "first attr");
request.getRequestDispatcher("file1.jsp").forward(request, response);
And suppose in the first JSP (file1.jsp), you are including another JSP (say file2.jsp):
<body>
.....
<jsp:include page="file2.jsp"></jsp:include>
.....
</body>
Then in file2.jsp, you can access attr1 by calling the request object's getAttribute method:
<body>
....
${attr1}
....
</body>
In case of jsp:include action, when file2.jsp is included in file1.jsp, both request and response objects are passed to file2.jsp as parameters. So file2.jsp can access the attributes of file1.jsp's request object.

How to use jsp value in servlets?

i want to use JSP values in servlet. iam fetching some parameters from the url and i need to pass those parameters in my servlet and also the servlet result something by using those parameters and i want to use the result again in the same jsp and in other jsp.
i get those parameters first time when my home jsp is called.
i am getting url parameter by using jstl core tags:
<c:set var="data" scope="session" value="${param.urldata}" />
How do i do this using JSP tags???
Thanks in advance
As i understand somewhat , you are trying to send the values from JSP page to Servlet..
For this we have lot of solution
Pass the value through method
Set the value at session and get it wherever you want in the whole application
I prefer second point, because which hold the value for the entire application
Check Pass variables from servlet to jsp
Above question pass the value from servlet to JSP,Your question is opposite of the answer that's it..!
Set value at JSP : session.setAttribute("key","value");
Get value at Servlet: session.getAttribute("name");
Also refer : Sharing values between servet and JSP

How does "request" represents an instance of HttpServletRequest in a JSP?

We often do the following inside the scriptlet:
<%
request.getCookies();
%>
Can anyone tell me, what is request here? I know it represents HttpServletRequest but I haven't declared it. I just do not understand this.
request is a variable of type HttpServletRequest that is declared and initialized for you in the code generated by the servlet container when the JSP code is transformed into a Servlet class and compiled, it also known as one of implicit objects in JSP.
Note that using scriptlets in JSP is bad practice for years. You should learn JSP EL, the JSTL, and choose an MVC framework that allows separating the Java code for the pure markup-generation code.

How to pass variable from a servlet to a jsp page?

I have a servlet (front-controller), which analyse the request, prepare some necessary data (model) and then should pass it to the jsp to be rendered.
How should I pass the data from servlet to jsp? (I hoped that it was possible to add new parameters to parameters map in the request object, but that map is unmodifiable).
I can add attributes to the request but I don't know how to retrieve them from the jsp.
All the data should be in the request scope. What's the correct way?
I can add attributes to the request but I don't know how to retrieve them from the jsp.
You don't need to specifically 'retrieve' them, just referring them works
request.setAttribute("titleAttribute", "kittens are fuzzy");
and then
Title here: ${titleAttribute}
You can use the request or the session scope for this. Apart from the answer by Nikita Rybak, you can use
request.getSession().setAttribute("key","value");
And then use it in JSP using scriplet.
<%=session.getAttribute("key")%>
Note that in the example given by Nikita, Expression Language(EL) has been used(I am not sure if it's JSTL tags). You need to explicitly state that EL is not to be ignored by using the isELIgnored property.
<%# page isELIgnored="false" %>

How do I redirect to a html page and pass variables to that page in Java?

I have a form on my index.html page which makes a POST request to a Java Servlet. This servlet does some processing and I would like to redirect back to index.html with some variables that the servlet has produced.
In PHP, it would be as simple as:
header("Location: index.html?var1=a&var2=b");
How can I acheive the same with Java, hopefully making use of a GET request.
Thanks all
In a Java Servlet, you'll want to write:
response.sendRedirect("index.html?var1=a&var2=b...");
Oh right, I should note that you'll want to do this in the processor method like doGet() or doPost()...
You redirect the response to the same servlet with some additional values:
req.setAttribute("message","Hello world");
rd =req.getRequestDispatcher("/index.jsp");
And in your servlet, you grab the data with:
<%=request.getAttribute("message");%>
You can use
HttpResponse.sendRedirect("Location: index.html?var1=a&var2=b");
See this link for more information.
It is as simple as :
response.sendRedirect("index.html?var1=a&var2=b");

Categories

Resources