So I have a .jsp that displays the header part of my web application. This is a very standard looking page. I have a bar at the top with things like Login, Register, Contact Us, etc... And below that we have a horizontal navigation bar.
This is a standard Spring web application with a maven build. It produces a file, web.war, that we deploy to tomcat. on dev, this works fine.
We are testing production, and want this war to be the root, so we rename the war to ROOT.war and restart. everything fine.
Now sometimes, we get the navigation section from a Content Management System. We set the text as a variable and display that. No obviously we can't use ${pageContext.request.contextPath} as that wont get parsed this way, so before we display the text we do this in the jsp
<% navcopy = navcopy.replace("${pageContext.request.contextPath}", request.getContextPath()); %>
Now this is adding the /web to the URL. Now this is baffling to me as registration link in the jsp works:
<a id="home-left-menu-item" href="${pageContext.request.contextPath}/register.html">Register Now!</a>
So hoe does ${pageContext.request.contextPath} parsed in the jsp return the correct root but the call request.getContextPath() returns "/web"
Aren't they essentially the same call to the same object to the same method?
Any insight would be appreciated.
${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/myProjectName/path/servlet
${pageContext.request.contextPath} returns /myProjectName
request.getServletPath() Returns the part of this request's URL that calls the servlet, e.g. /path/servlet
${pageContext.request.servletPath} returns /path/servlet
For this code try this:
<a id="home-left-menu-item" href="<%=request.getContextPath()%>/register.html">Register Now!</a>
Related
In my application, the http://localhost:8080/TestApplication/subCategories/2 will display subcategories of my table with id 2.
Click Here
When I click on the link rendered by the HTML above, my server is redirecting to http://localhost:8080/SecondOpinion/subCategories/hello
I want it to redirect to
http://localhost:8080/SecondOpinion/hello
How do I achieve that?
First of all, this is nothing to do with "anchor tags". An anchor tag is an HTML element of the form <a name="here">, and it defines a location within the HTML that another URL can link to.
What you have is an ordinary HTML link, and what you are seeing is standard HTML behavior for a relative link.
A relative link is resolved related to the "parent" URL for the page containing the link.
If you want a link to go somewhere else, you can:
Use an absolute URL
Use path in the relative link; e.g. `Click Here
Put a <base href="..."> element into your document's <head> section.
In your case, you seem to be1 combining relative URLs with some unspecified server-side redirection. In this case, you could either:
change as above, so that the URL that is sent to the server (before redirection) goes to a better place, or
change the redirection logic in your server.
I can't tell which would be more appropriate.
1 - I am inferring this because you said "my server is redirecting to". It is possible that you actually mean that the browser is sending that URL to the server, and there is no redirection happening at all.
I have a search.jsp page that has some html content and a form. When the form is submitted, there is a servlet handle the form data and forward the results to the search.jsp page. However, the url in the browser after processing the form is changed to the servlet name:
http://localhost:8080/MyProject/SearchServlet
not the search.jsp page:
http://localhost:8080/MyProject/Search.jsp
How I can change the url to the search.jsp? In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?
You cannot do that by forwarding the request: you need to "tell" the browser to generate a new http request by using the response.sendRedirect() method.
Now the question is why do you want the url bar to display the name of the Jsp?
Hiding the real destination path is a desired feature when forwarding requests: users do not need to know the server side redirects (that's how they are also called) happening in your web app.
Think about it: to carry out its tasks a servlet potentially can forward the request a number of times before getting to the final destination: you don't want the url bar to change each and every single time.
Give a fancier name to your servlet like: "Search" rather than "SearchServlet" so that users will know they are on the search page of your web application and not in the "SearchServlet" page.
In addition to that, if you visit any professional website, you will hardly ever see the .jsp or .html or .php extension on the address bar. While that is not a requirement or specification and you are free to do so, I believe the first approach is best practice (it looks even better to me honestly). There is even a folder WEB-INF whose purpose is to hide your .jsp pages from direct access via url bar.
What I like doing is having a servlet as the landing-welcome page of the web app, that will be responsible to forward and redirect requests based on the user input and the inner working of the application.
Now back to your final request (pun intended)
"In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?"
What I would do is:
redirect the user to the "Search" servlet from the welcome/home servlet.
In the doGet method of the Search servlet I would forward the request to the search.jsp page (you could set attributes before forwarding if you need to).
In the search.jsp I would set the action attribute of the form to "Search" (the name of the servlet) and the method to POST.
In the doPost method of the Search servlet you would implement whatever logic you wish to implement and finally forward the request to the search.jsp
After hitting the search button (and even after the submit button is clicked) what the user will see on the address bar is simply
http://localhost:8080/MyProject/Search
Hope that makes sense.
Are you using the same search.jsp for searching and well as showing the result? It is possible to use the same jsp to perform both the functions but it's easier and desirable to make another jsp which will only show the results.
If you are not able to see the results on search.jsp then make sure that you are setting the proper response in the Servlet class before forwarding it to the jsp and also whether you are reading the response sent by the Servlet class properly in the jsp.
If you want, the page to not refresh at all, then go for AJAX.
I have developed a theme in liferay 6.1. I have a page named "localhost:8080/home" but now i want that on clicking this link of the page, it should be redirected to localhost:8080
Any suggestions are welcomed.
Thanks in Advance.
I think you are confused a little bit, so just some things you should know:
You can't (normally and without hacks) have a page named "localhost:8080". Every Page (or 'Layout' in Liferay) has a short name, that takes it's part of the url. This is often called "friendly url" but it's often confused with the "friendly url feature", which is a way to shorten your url request data.
So you're always going to have urls like 'localhost:8080/something'. The same holds for the 'home' page
You can partially shorten the Url by using 'virtual host'. It removes the part of the url before your page's name (like removing the web/guest or user/username ) suffix
You can use the 'friendly url' feature to shorten the part of the url that goes after the page's name, and contains request information like lifecycle state info or custom request parameters
To remove the language toggle from the page view(Comfirmation Page)
I found this code but it doesn't work in Spring MVC
<c:if test="${!fn:contains(pageContext.request.servletPath,'/comfirmation')}">
//Other Code
</c:if>
My actual url is (ShoppingCart.jsp).
It is used when /viewCart.htm,/updateCart.htm,/Confirmation.htm,etc.
So, the user go to the /Confirmation.htm, it also redirect to the ShoppingCart.jsp but the url path in the browser is /Confirmation.htm.
I want to remove the language toggle when call the /Confirmation.htm in the above mention.
Finally, I got it. Here we go
<%
String url=request.getAttribute("javax.servlet.forward.servlet_path").toString();
if(url.equals("/Confirmation.htm")){
%>
//Language Toggle code
<% } %>
I decided to use this. Another way is that storing url path in session since front controller.
the pageContext.request.servletPath will give you the path of the jsp (and not the url your browser shows).
The request is forwarded to a controller, which returns a path to a view. The view ist called using a second internal request
I'm using netbeans to create a web application that allows a cell phone user to scan a QR code and retrieve a journal's previous/next title information. The take home point is that there's no form that the user will input data into. The QR code will contain the search string at the end of the URL and I want the search to start on page load and output a page with the search results. For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. Oh, to top it off...I'm brand new to programming in java and using netbeans. I have no clue about what javabeans are, or any of the more advance techniques. So, with that background explanation here's my question.
I have created a java class (main method) that parses my xml and retrieves the results correctly. I have an index.jsp with my html in it. Within the index.jsp, I have no problem using get to get the title information from my URL. But I have no clue how I would get those parameters to a servlet that contains my java code. If I manage to get the search string to the servlet, then I have no idea how to send that data back to the index.jsp to display in the browser.
So far every example I've seen has assumed you're getting form data, but I need parameters found, processed and returned on page load...IOW, with no user input.
Thanks for any advice.
Ceci
Just put the necessary preprocessing code in doGet() method of the servlet:
String foo = request.getParameter("foo");
// ...
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
And call the URL of the servlet instead of the JSP one e.g. http://example.com/page?foo=bar instead of http://example.com/page.jsp?foo=bar in combination with a servlet mapping on an URL pattern of /page/*.
You can get the url parameter in servlet using request.getParameter("paramName");
and you can pass the attribute to page from servlet using forwarding request from servlet to jsp.
See Also
Servlet wiki page
Bear in mind that your JSP page will compile internally to a servlet. So you can retrieve the string and print it back in the same JSP. For instance assuming you get the string in a parameter called param you would have something like this in your JSP:
<%
String param = request.getParameter( "param" );
out.println( "String passed in was " + param );
%>
One last thing, you mentioned the main method -- that only gets executed for stand alone applications -- whereas you're talking web/JSP :O