login

Steps:

  1. Create login.html
  2. Create servlet that extends HttpServlet
  3. Create web.xml and configure servlet
  4. Create war file and deploy the application.

Create login.html

<html>
<head>
<title>Login Page</title>
</head>
<body>
	<form action="loginServlet">
	<h1 align="left">HttpServlet</h1>
		<table>
			<tr>
				<td>Name:</td>
				<td><input type="text" name="uname">
				</td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit">
				</td>
			</tr>
		</table>

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

Create servlet that extends HttpServlet

package com.java2learn;

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 LoginDemo extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//Getting PrintWriter object to write data to the browser.
		PrintWriter out=resp.getWriter();
		//Getting form parameter
		String name=req.getParameter("uname");
		out.println("Hello "+name+" doGet() method is called");
		
	}

	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		String name=req.getParameter("uname");
		out.println("Hello "+name+" doPost() method is called");
		
	}
}

Create web.xml and configure servlet

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>WebApp</display-name>

	<servlet>
		<servlet-name>LoginDemo</servlet-name>
		<servlet-class>com.java2learn.LoginDemo</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LoginDemo</servlet-name>
		<url-pattern>/loginServlet</url-pattern>
	</servlet-mapping>

</web-app>

Final application structure.

LoginDemo

Create war file and deploy the application

Output:

loginHtml

OutputLogin

Download project: WebApp