Skip to content

Instantly share code, notes, and snippets.

@ats1999
Last active April 9, 2020 19:07
Show Gist options
  • Save ats1999/2ac0059085340e68c749c377bb2fadf7 to your computer and use it in GitHub Desktop.
Save ats1999/2ac0059085340e68c749c377bb2fadf7 to your computer and use it in GitHub Desktop.
WADD
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Demo</title>
</head>
<body>
<form action="demo" method="post">
<h1 style="font-size:40px;">Login : </h1>
Enter User name:
<input type="text" name='user' placeholder="Enter user name" value="Rahul"><br><br>
Enter number:
<input type="number" name="number" placeholdr="enter number" value="9507271781"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

File

  • ServletDemo.java This file contains all the method required to fulfill client requirements.
  • index.html This file called, when client request through url for the first time.
  • web.xml This file is containg all the mapping information of servlet and url's information.

Demo:- When user visit the URL

img

After request served by servlet.

package number;
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;
/**
*
* @author Rahul kumar
* @version 1.0
* @category Assingment
* @see This program is written to test and execute servlet.
*/
public class ServletDemo extends HttpServlet{
/**
* This method is by default called by Tomcat.
* @param nothing
* @return nothing
*/
public void init(){
// do notheing
}
/**
* This method is called only, whena client request for this servlet by POST request.
* @param req res
* @return text/html
* @throws ServletException IOException
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
// get writer
PrintWriter pw=res.getWriter();
// set response type
res.setContentType("text/html");
// get parameter
String name=req.getParameter("user");
String num=req.getParameter("number");
// send output to the client
pw.print("<h1> Hi, "+name+" Your mobile number is: "+num);
}
/**
* This method is called by servlet container. It is part of servlet lifecycle.
* @param nothing
* @return nothing
*/
public void destroy(){
// do nothing
}
}
<?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_3_0.xsd" version="3.0">
<display-name>Mobile number</display-name>
<!-- servlet to take input from user -->
<servlet>
<servlet-name>Demo</servlet-name>
<servlet-class>number.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment