welcome.html
[java]
<html>
<body>
<form action="./welcome">
user::<input type="text" name="user"/>
fullname::<input type="text" name="fullname"/>
<input type="submit" value="go"/>
</form>
</body>
</html>
[/java]
Here the developer just created two text boxes, one for User and another for full name and also created a submit button that is
go.
web.xml
[java]
<web-app>
<servlet>
<servlet-name>Demo</servlet-name>
<servlet-class>ServletRequest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
[/java]
Here make sure that servlet name should be same and URL pattern should be match with HTML form action
URL.
ServletRequest.java
[java]
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletRequest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("");
String username = request.getParameter("user");
ServletConfig config = getServletConfig();
String fullname = request.getParameter("fullname");
out.print(" <h1><b>Hi "+username+" welcome to Splessons</b></h1>");
out.print("<h2><b>This is your fullname "+fullname+" </b></h2>");
out.print("");
out.flush();
out.close();
}
}
[/java]
Where
ServletRequest is the class name, this class is extending
HttpServlet , In doGet Method the parameters are appended to the URL and sent along with header information,
DoGet is faster if we set the response content length since the same connection is used.
Output:
Compile the code from HTML page then the following output will be displayed.
After entering the data to the above form result will be as follows.
Difference Between System.out.println() and PrintWriter
Both are used to display the output. But
system.out.println is used in java and
out.prinln (PrintWriter) is used in scripting languages. An user will create the object for the class PrintWriter as "out". out.println() is used in JSP. If an user create object for that PrintWriter class as "splesson", user have to print line as
splesson.println().