Servlet_has_to_accept_input_from_the_client_-Notes_lyst4960
Servlet_has_to_accept_input_from_the_client_-Notes_lyst4960
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
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 ‘&’.
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;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("name = "+name);
System.out.println("designation = "+desig);
System.out.println("tech skills = "+ts);
}
}
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
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-
<!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
After deploying the server and executing the code in the browser
Let us now see how to pass Relative Path in the action field:
<form action="Serv1">
Output: