4

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&paramName2=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:

Directory structure

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.

2 Answers 2

3

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");
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply. I don't want to access the parameter. I would like to add the parameters in the response.
Removed what I edited. Didn't fully understand that you only want to add the parameters to the requested URL. Have to think about this.
I have the "searchPage.html" file listed in the welcome-file list in web.xml. So whenever I run the web app, the searchPage.html is opened. Now I want some parameters to appear after the url.
Hello Jurgen, so I removed the "searchPage.html" from the welcome-file list. Now I'm generating the html page on the fly using the xslt stylesheet. Now whenever the user opens the link: localhost:8080/HeadersTest, they are redirected to the other url which contains the parameters: localhost:8080/…. I did it using response.sendRedirect("url with parameters"). I believe you gave this solution to me before you edited your answer.
0

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.

3 Comments

Thanks for your reply Abhijeet. I don't want to access the parameters. I would like to add the parameters in the response so that they appear in the url. I tried doing response.addHeader("paramName1", "paramValue1") but the parameter didn't appear in the url.
If you don't want to use parameters then why are you passing it in url?
I am doing that already for the subsequent requests. I just need to add the parameter for the first time the html page launches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.