I’m working on Java Servlets web application. I have a html file “searchPage.html” in the WebContent folder. I have included the “searchPage.html” name in the welcome-file list of web.xml. Now whenever I run the servlet, the searchPage.html is run. The url is
http://localhost:8080/HeadersTest/ .
“HeadersTest” is the name of the web app. Now my question is, I would like to add some parameters in the url following the “HeadersTest”.The parameters shall appear after the web app is run. Do I need to add these parameters in the service methods (doGet, doPost etc.)? For example:
http://localhost:8080/HeadersTest?paramName1=paramValue1¶mName2=paramValue2.
I’m fairly new to servlet. If someone can point me in the right direction, that will be really helpful. I have attached the screenshot of my directory structure of my web application below:
Update:
As I have listed the "searchPage.html" in welcome-file list of web.xml, "searchPage.html" launches whenever I run the web application. I would like to add few parameters in the url when the web app launches.
Adding the parameters to the URL means this is a GET request.
just handle it in your servlet's doGet() method:
request.getParameter("paramName1");
When you want to show URL Parameters you could just go with
response.sendRedirect("url with parameters");
Generally, We pass parameters in url if you want to access those parameters in Servlet/Controller side.
If you want to use these parameters in your controller which is nothing but your MainServlet class then you should pass these parameters in url. You can access these using
request.getParameter("paramName1")
in your doGet() or doPost() method.
Related
The use case I am trying to implement.
Display different content based on the {{random_string}} in the URL path.
Users will see different content based on the {{random_string}} that the URL contains.
eg:
www.example.com/{{random_string}}/index.jsp
The URLS will look like these below. (
They include random characters before the JSP)
www.example.com/xc/index.jsp www.example.com/2b/index.jsp
www.example.com/43/index.jsp
My question
How do I setup nginx and tomcat to be able to support the {{random_string}} in the URL without throwing 404?
My Current Environment/Setup (this works fine)
Nginx along with Tomcat.
Requests that come to nginx are then redirected to tomcat to access ROOT.war
e,g - www.example.com/index.jsp
You shouldn't have to change anything in Nginx or Tomcat config. What you could do is to create a servlet that will intercept the requests and extract the {{random_string}} before forwarding to the JSP. Here are the basic steps:
1) Create a servlet with a URL pattern of /* so that all requests go to it.
2) In the servlet's doGet() method, use request.getPathInfo() to retrieve the URL path and parse it to extract the {{random_string}}.
3) Use request.setAttribute() to set attributes for the data you want to display in the JSP page.
4) Forward the request to the JSP using a RequestDispatcher, e.g.:
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
5) In the JSP, use the request attributes that you have set in step 3 to display the content.
I would like to read jsp page from my application and save it to a file - it's output, not the code itself. Plus, my application has basic authentication (username+password).
If it was a Servlet, I could just access it's doGet method.
One solution I've found is this - Open URL connection, provide authentication details and read the stream.
I'm wondering if there's another option, maybe accessing the generated Servlet in the web container and then using reflection to call the class doGet.
You can precompile the JSP and then call the servlet (you don't have to use reflection even).
If you try to call the JSP's servlet without precompiling then it might not exit yet (because usually the server only compiles the JSP after it was called for the first time).
To precompile the JSP, check your web server documentation.
Personally I think you're better of using URL connection. Precompiling JSPs is not portable (as in you need to do it in a different way for each web server).
Edit
You can also use RequestDispatcher.include() method with a wrapped response object as described in this answer.
I wrote custom servlet in Liferay and want to know which user page calls it and know other parameters like theme. But the request's attributes and session fields are all nulls.
How to make custom servlet to receive request as if portlet does?
Thanks
P.S. I don't want to use this solution https://www.everit.biz/web/guest/blog/-/blogs/getting-current-liferay-user-in-a-standalone-webapp?_33_redirect=/web/guest/blog
which reads cookies manually. I want to do such as Liferay does, i.e. by using it's API. Is it possible?
Update 1.
I have a portlet and a servlet in one WAR. I can know who am I (logged in user) from within portlet JSP like this:
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
themeDisplay.getUser()
Now I want to do the same from a servlet. Is it possible?
I am working in eclips which deploys automatically.
You either have to mimic what Liferay does in the portlet request handling (not recommended) or, alternatively, put your servlet code into a portlet - this can be the "resource handling" of a portlet - here you get full access to the http request and can do everything yourself with regards to data types transmitted in the stream.
I'd rather recommend this as it will be significantly easier to upgrade. Portlet Resource Handler are very similar to servlets from a logical point of view. There might be other (more advisable) options, but this is what comes to my mind for this type of problem.
I am trying to understand how the requests works. Unfortunately I was thrown at coding first and only then at understanding.
I wrote some really basic webapplication in java few years ago and it did work as expected. On its main web-page(.jsp) I had following as one of the menu buttons:
<p>test</p>
I am currently writing new webapp and forgot a lot. This time I am doing it with Spring MVC and properly. I can't really understand why this snippet no longer brings me to the home.jsp in current webapplication and why at first I did use it in old app.
Apache gives: The requested resource () is not available.
It is not that I need that sort of direct interaction, it is just I am trying to understand whether resources are accessible via URL? Does Spring MVC brings me extra security, where only servlet handled requests can result in a view? Am I missing something really trivial?
Moreover in that same old web app menu I had direct link to the servlet, but currently I can't make such direct reference to the servlet in the new webapp. I can make relevant request which will be captured by the servlet, but not by the name of it.
Apache gives: The requested resource () is not available.
Reference to servlet from menu:
<% if((String) session.getAttribute("passengerFound") != null){ %>
<img style="border:0" src="menuButtons/My Trips.png" alt="My Trips"/> <%} %>
Thanks, I bet it is really simple. I really want to understand, please help.
I know that it has something to do with Front Controller(dispatcherServlet), but I can't form logical and firm explanation in my head.
it is just I am trying to understand whether resources are accessible
via URL
In short, no. The default behavior and recommended configuration when using Spring MVC is to map the Spring DispatcherServlet to the / url pattern, meaning ALL requests are sent to the DispatcherServlet. Out of the box, the dispatcher-servlet will NOT service any requests for static resources. If this is desired, the two main options are
Map the DispatcherServlet to another pattern than root, effectivly isolating the Spring MVC portion to a sub-context
Add a resource-mapping to your spring context (your applicationContext.xml).
<mvc:resources mapping="/res/**" location="/res/" />
This above would tell spring mvc to treat all request to /res/** as requests for static resources (like images etc) and that those resources are physically located in the /res/ folder in the application root.
You might just be missing a "/" as in "/home.jsp" instead of "home.jsp"
UTIL.getServletPath("/SetupPage?PG=setupusersettings") %>">
What is SetupPage. Is it an directory?
Why ? is used?
What is PG?
setupusersettings is an jsp page, all i can find and it will redirect to that page.
you need to learn the basics of web programming. You can find millions of stuffs on Web about it. Shortly;
/SetupPage?PG=setupusersettings
SetupPage seems like a web application, you requested. Basicly, as soon as you call /SetupPage, the request will be sent to this application.
"?" is a seperator. after that, you can send parameters (you can send parameters using HTTP GET/POST) to this application. In this case, the name of parameter is PG and the value "setupusersettings". For exampe, you can access the value of this parameter in your web application
Google "Web programming java servlet tutorial"
Regards.
If you click on the resulting link (URL) it will (literally) "call the resource /SetupPage with the attribute/value pair [PG -> setupusersettings]".
PG is an attribute name (could be an acronym for Page).
Look for a resource named "SetupPage.???" (could be a python/perl script or any other executable file). You may have to look at the http servers configuration files to get the local location of this file. The redirection to is most probably done by this executable.