______________________________________________________________________________
HelloServlet.java
______________________________________________________________________________
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by yubraj on 11/6/16.
*/
@WebServlet(urlPatterns = {"/HelloServlet"})
public class HelloServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
________________________________________________________________________________
LogonServlet.java
______________________________________________________________________________
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by yubraj on 11/6/16.
*/
@WebServlet(name = "LogonServlet", urlPatterns = {"/LogonServlet"})
public class LogonServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
if(username.equals("user") && password.equals("pass")){
out.print("Welcome user");
}else {
out.print("Wrong UserId or Password! Please try again");
out.println("<form method=POST action=LogonServlet>");
out.println("Username=<input type=text name=username> <br>");
out.println("Password=<input type=text name=password>");
out.println("<input type=submit value='Logon'>");
out.println("</form>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<form method=POST action=LogonServlet>");
out.println("Username=<input type=text name=username> <br>");
out.println("Password=<input type=text name=password>");
out.println("<input type=submit value='Logon'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
______________________________________________________________________________
guessNumber.java
______________________________________________________________________________
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
/**
* Created by yubraj on 11/6/16.
*/
@WebServlet(name = "guessnumber", urlPatterns = {"/guessnumber"})
public class guessnumber extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
PrintWriter out = response.getWriter();
int num = Integer.parseInt(request.getParameter("number"));
int randNumber = Integer.parseInt(session.getAttribute("randNum").toString());
out.println("<html>" +
"<head>" +
"<title>Guess Number Game</title>" +
"</head>" +
"<body>");
if(num == randNumber){
out.println("Congratulation you won");
out.println("<br><a href='/guessnumber'>Try Again</a>");
}else if(num>randNumber){
out.println("Number is too high. Try Again!");
out.println("<br><a href='/guessnumber'>Try Again</a>");
}else{
out.println("Number is too low. Try Again!");
out.println("<br><a href='/guessnumber'>Try Again</a>");
}
out.println("</body></html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Random rand = new Random();
int x = rand.nextInt(10);
HttpSession session = request.getSession(true);
session.setAttribute("randNum", x);
PrintWriter out = response.getWriter();
out.println("<html>" +
"<head>" +
"<title>Guess Number Game</title>" +
"</head>" +
"<body>" +
"Enter the number between 1 and 10"+
"<form method=POST action=guessnumber>" +
"<input type=number name=number />" +
"<input type=SUBMIT value=Enter />" +
"</form>"+
"</body></html>");
System.out.println(x);
}
}
------------------------------------------------------------------------------------------------------------
Web.xml
------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
</web-app>