Java Servlets

drivercutInternet and Web Development

Nov 13, 2013 (3 years and 4 months ago)

95 views

CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 1 of 13

Java Servlets Example 1 (Plain Servlet)
SERVLET CODE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Servlet1", urlPatterns = {"/Servlet1"})
public class Servlet1 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>My first servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Welcome to Servlets!</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}


CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 2 of 13

HTML PAGE CODE
<html>
<head>
<title>An HTML page to invoke the welcome Servlet</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="Servlet1" method="get">
<label> Click on the button to invoke the servlet</label>
<input type="submit" value="CLICK HERE"/>
</form>
</body>
</html>







CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 3 of 13

Example 2 (Servlet with form data using GET)
SERVLET CODE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Servlet2", urlPatterns = {"/Servlet2"})
public class Servlet2 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>My second servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Extracting data using get request</h1>");

// Get the data
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String age = request.getParameter("age");

out.println("Hello " + name + " " + surname + "!<br/>");
try{
int ageInt = Integer.parseInt(age);
CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 4 of 13

if(ageInt < 18){
out.println("You cannot drive");
}else{
out.println("You can drive");
}
}catch(Exception e){
// Do nothing
}

out.println("</body>");
out.println("</html>");
out.close();
}
}

HTML PAGE CODE
<html>
<head>
<title>An HTML page to invoke the Servlet using a get</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Fill in the following form</h1>
<form action="Servlet2" method="get">
<label>NAME: </label> <input type="text" name="name"/>
<br/> <br/>
<label>SURNAME: </label> <input type="text" name="surname"/>
<br/><br/>
<label>AGE: </label> <input type="text" name="age"/>
<br/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 5 of 13










http://localhost:8084/MyWebAplication/Servlet2?name=Matthew&surname=Xuer
eb&age=26

CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 6 of 13

Example 3 (Servlet with form data using POST)
SERVLET CODE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Servlet3", urlPatterns = {"/Servlet3"})
public class Servlet3 extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>My second servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Extracting data using get request</h1>");

// Get the data
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String age = request.getParameter("age");

out.println("Hello " + name + " " + surname + "!<br/>");
try{
int ageInt = Integer.parseInt(age);
CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 7 of 13

if(ageInt < 18){
out.println("You cannot drive");
}else{
out.println("You can drive");
}
}catch(Exception e){
// Do nothing
}

out.println("</body>");
out.println("</html>");
out.close();
}
}

HTML PAGE CODE
<html>
<head>
<title>An HTML page to invoke the Servlet using a post</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Fill in the following form</h1>
<form action="Servlet3" method="post">
<label>NAME: </label> <input type="text" name="name"/>
<br/> <br/>
<label>SURNAME: </label> <input type="text" name="surname"/>
<br/><br/>
<label>AGE: </label> <input type="text" name="age"/>
<br/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 8 of 13














http://localhost:8084/MyWebAplication/Servlet3

CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 9 of 13

Java Server Pages (JSP) Example 1
JSP Code
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date" %>

<html>
<head>
<title>JSP Page showing the current date and time</title>
</head>
<body>
<h1>Date and time</h1>
Current time: <%= new Date() %>
<br/><br/>
Date only:
<%
Date now = new Date();
String fullDateAndTime = now.toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
out.println(dateFormat.format(now));
%>
</body>
</html>









CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 10 of 13

Generated HTML code <html>
<head>
<title>JSP Page showing the current date and time</title>
</head>
<body>
<h1>Date and time</h1>
Current time: Fri Aug 17 11:40:31 CEST 2012
<br/><br/>
Date only:
17/08/2012

</body>
</html>









CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 11 of 13

Example 2
HTML Code
<html>
<head>
<title>An HTML page to invoke a JSP page</title>
</head>
<body>
<h1>Times table</h1>
<form action="TimesTable.jsp" method="get">
<label>Enter a number to display it's times table: </label>
<input type="text" name="num"/>
<br/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>






CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 12 of 13

JSP Code
<html>
<head>
<title>JSP Page showing the times table</title>
</head>
<body>
<%! int num = 1; %>

<%
String sNum = request.getParameter("num");
try{
num = Integer.parseInt(sNum);
}catch(Exception e){
// Do nothing
}
%>

<h1>The <%=num%> times table</h1>

<%
for(int i = 1;i <= 10;i++){
out.println("<br/>" + num + " x " + i + " = " + num*i);
}
%>
</body>
</html>








CIS 3952 [Part 2] Java Servlets and JSP tutorial
Matthew Xuereb © 2012/2013 Page 13 of 13

Generated HTML code <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page showing the times table</title>
</head>
<body>
<h1>The 5 times table</h1>
<br/>5 x 1 = 5
<br/>5 x 2 = 10
<br/>5 x 3 = 15
<br/>5 x 4 = 20
<br/>5 x 5 = 25
<br/>5 x 6 = 30
<br/>5 x 7 = 35
<br/>5 x 8 = 40
<br/>5 x 9 = 45
<br/>5 x 10 = 50
</body>
</html>