A Coder Gamer
A Coder Gamer

Reputation: 840

Servlet url parameter

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.

Upvotes: 4

Views: 11228

Answers (2)

Jürgen
Jürgen

Reputation: 418

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");

Upvotes: 3

Abhijeet
Abhijeet

Reputation: 4309

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.

Upvotes: 0

Related Questions