AndreaNobili
AndreaNobili

Reputation: 42957

How to retrieve the value of an Http Request parameter into a Servlet?

I am absolutly new in J2EE and this if my first time that I implement an HttpServlet

In my web.xml file I have putted the following configuration:

<servlet>
    <servlet-name>salwf</servlet-name>
    <servlet-class>it.sistinf.ediweb.monitor.servlets.Salwf</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>salwf</servlet-name>
    <url-pattern>/salwf.do/*</url-pattern>
</servlet-mapping>

So this servlet is performed for the /salwf.do/* pattern into the HTTP REQUEST.

Into the service() method of my servlet I found something like it:

String service = req.getParameter("serv");

So what exactly do this line? I think that it retrieve the value of a GET paramether named serv inside the HTTP Request.

So for example if in my browser I put something like it:

http://localhost:7001/salwf.do/myPage?serv=1

it is retrieved the "1" value of the serv parameter?

Is it correct or am I missing something?

Tnx

Upvotes: 0

Views: 2165

Answers (1)

Liam de Haas
Liam de Haas

Reputation: 1268

Your corrrect that if you have tthe GET parameter ?serv=1 that req.getParameter("serv") will return the value of the parameter (in this case 1) as a String.

So if you do what you have, String service = req.getParameter("serv"); and the request has a GET parameter that looks like this ?serv=1 then the value of service is now "1".

In your case the <servlet-mapping> in web.xml is incorrect. You should remove the /* after /sawlf.do

Looks like this:

<servlet-mapping>
    <servlet-name>salwf</servlet-name>
    <url-pattern>/salwf.do</url-pattern>
</servlet-mapping>

And then if you want to send a request to the servlet the url should look like this: http://localhost:7001/salwf.do?serv=1

Upvotes: 2

Related Questions