0% found this document useful (0 votes)
15 views

Servlet_has_to_accept_input_from_the_client_-Notes_lyst4960

Uploaded by

Adityareddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Servlet_has_to_accept_input_from_the_client_-Notes_lyst4960

Uploaded by

Adityareddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Day - 4

Scenario - 1
Servlet has to accept input from the client

In the previous session we had seen how the client was connecting to our Server via the URL but what if
we wanted to pass input to the server from the client?
There are two ways through which we can pass input to the server-
● Query String
● HTML form

Let us see how Query String works


Through Query String we can pass input to the server and we pass the input after the URL
by entering a ‘?’ after the URL suggesting that Query String starts from here.
Query String is nothing but the ability to pass data to the servlet in the form of key & value
pairs.
For Example-

So the data we enter after ‘?’ will be considered as Query String and if we want to pass multiple
key and value pairs then we have to separate them with ‘&’.

So how does Query String reach the Servlet?


We know that whenever we pass something to the servlet via the browser, it will sent in the
RequestFormat and the data in the Query String will be present in the body of the RequestFormat
Now the RequestFormat will reach the Deployment Descriptor(web.xml). The web.xml will now map to
the Servlet based on the url-pattern mentioned in the URL.
We know that before the service method (doGet()) is called, two objects will be created i.e., Request
object and Response object and the service method will access to both these objects.
The RequestFormat will be placed inside the request object and with the help of the following methods we
can access the data inside the body of the RequestFormat
● getParameterNames( )
● getParameterValues( )
● getParameter( )
● getParameterMap( )

Let us try to implement these methods in our code

package com.tap.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String name = req.getParameter("name");


String desig = req.getParameter("desig");
String ts = req.getParameter("techskills");

System.out.println("name = "+name);
System.out.println("designation = "+desig);
System.out.println("tech skills = "+ts);
}
}

And if we start the server and execute the code -


Output:

Let us explore the other methods-

protected void doGet(HttpServletRequest req, HttpServletResponse resp)


throws ServletException, IOException {
Enumeration<String> pN = req.getParameterNames();
while(pN.hasMoreElements()) {
System.out.println(pN.nextElement());
}
}

And we will restart the server and deploy it in the browser


Output:

What if the client will send multiple values for a single parameter?
We can use getParameterValue( ) whenever a single parameter has multiple values and those values will
be saved in a String array

protected void doGet(HttpServletRequest req, HttpServletResponse resp)


throws ServletException, IOException {

String name = req.getParameter("name");


String desig = req.getParameter("desig");
String[] pV = req.getParameterValues("techskills");

System.out.println("name = "+name);
System.out.println("designation = "+desig);

for(String i : pV) {
System.out.println(i);
}

Output:
Now let us see how we can take input from the HTML forms-
Whenever a user requests for “http://localhost:9090/J2EE/”, then automatically index.html should be sent
as response and a html form should be displayed like this-

So let us now create the HTML form.


1. Open index.html file and type in the following code-

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h3>Enter your details</h3>
<form action="">
<label>name:</label>
<input type="text" name="name"><br/>

<label>designation:</label>
<input type="text" name="desig"><br/>

<p>Techskills:</p>
<input type = "checkbox" name="techskills" value="java">
<label>Java</label><br/>
<input type = "checkbox" name="techskills" value="python">
<label>Python</label><br/>
<input type = "checkbox" name="techskills" value="C++">
<label>C++</label><br/>
<input type = "checkbox" name="techskills" value="C#">
<label>C#</label><br/>
<input type = "checkbox" name="techskills" value="Javascript">
<label>Javascript</label><br/><br/>

<input type="submit">

</form>
</body>
</html>

Now when the user requests for index.html and after entering all the details in the form and clicks on the
Submit button, then automatically the details must be sent to FirstServlet.java servlet.
So how do we achieve that?
That is where the “action” field in the html file comes into picture.
We have to mention the URL to which the request must be sent when someone clicks on the Submit
button.
We can mention the URL in two ways-
● Absolute Path
● Relative Path

We will how to give Absolute path-


<form action="http://localhost:9090/J2EE/Serv1">

After deploying the server and executing the code in the browser

And enter the following details and click on submit button-


We can see that automatically a Query String will be generated and output will be displayed on the
console

Let us now see how to pass Relative Path in the action field:
<form action="Serv1">
Output:

You might also like