In this tutorial we will learn how to print the user input data with form or we can say that how to take input from user and print that input data. Let see step by step code for the requirement.
When we click on Submit the following user Input Data will be Displayed as output...
index.jsp
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Servlet Form Data</title>
</head>
<body>
<form action="dash"
method="get">
Name:<input type="text" name="name"/><br>
Address : <input type="text"
name="add"/>
<input type="submit"
value="Submit">
</form>
</body>
</html>
A.java
package com.dashzin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class A extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException{
res.setContentType("text/html");
PrintWriter
out=res.getWriter();
String name=req.getParameter("name");
String Add=req.getParameter("add");
out.print("<h2>Your
name is</h2>"+ name);
out.print("<h2>Your
Address is</h2>"+ Add);
}
}
web.xml
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Test1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.dashzin.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/dash</url-pattern>
</servlet-mapping>
</web-app>
OutPut
when we will run this code, in this way we will get the output .......
here i have given the input "Name" and "Address".....When we click on Submit the following user Input Data will be Displayed as output...
kuşadası
ReplyDeletesiirt
van
hakkari
aydın
D224