FALLSEM2021-22 CSE1007 ETH VL2021220104880 Reference Material I 22-Oct-2021 1. Java Servlets
FALLSEM2021-22 CSE1007 ETH VL2021220104880 Reference Material I 22-Oct-2021 1. Java Servlets
JSWDK
Small, stand-alone server for testing servlets
and JSP pages
The J2EE SDK
Includes Java Servlets 2.4
Servlet capable server
Apache
Popular, open-source server
Tomcat
A “servlet container” used with Apache
<html>
<head>
</head>
<body>
<b>
JSP, ASP, CF, PHP - you name it, we support it!
</b>
</body>
</html>
</font>
<head>
</head>
<body>
<%
// jsp sample code
out.println(" JSP, ASP, CF, PHP - you name it, we support it!");
%>
</body>
</html>
<html>
<head>
</head>
<body>
<b>
JSP, ASP, CF, PHP - you name it, we support it!
</b>
</body>
</html>
</font>
<head>
</head>
<body>
<%
// jsp sample code
out.println(" JSP, ASP, CF, PHP - you name it, we support it!");
%>
</body>
</html>
<html>
<head>
</head>
<body>
<b>
JSP, ASP, CF, PHP - you name it, we support it!
</b>
</body>
</html>
</font>
<head>
</head>
<body>
<%
// jsp sample code
out.println(" JSP, ASP, CF, PHP - you name it, we support it!");
%>
</body>
</html>
<html>
<head>
</head>
<body>
<b>
JSP, ASP, CF, PHP - you name it, we support it!
</b>
</body>
</html>
</font>
Servlet Code
Written in standard Java
Implement the javax.servlet.Servlet
interface
package servlet_tutorials.PhoneBook;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.net.*;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Servlets
Client
Server
Saving State
Session Tracking
A mechanism that servlets use to maintain
state about a series of requests from the
same user (browser) across some period of
time.
Cookies
A mechanism that a servlet uses to have
clients hold a small amount of state-
information associated with the user.
Servlet Communication
To satisfy client requests, servlets
sometimes need to access network
resources: other servlets, HTML pages,
objects shared among servlets at the
same server, and so on.
Calling Servlets
Typing a servlet URL into a browser
window
Servlets can be called directly by typing their
URL into a browser's location window.
Calling a servlet from within an HTML
page
Servlet URLs can be used in HTML tags,
where a URL for a CGI-bin script or file URL
might be found.
Request Attributes and Resources
Request Attributes
getAttribute
getAttributeNames
setAttribute
Request Resources - gives you access to
external resources
getResource
getResourceAsStream
Multithreading
Concurrent requests for a servlet are handled by
separate threads executing the corresponding
request processing method (e.g. doGet or
doPost). It's therefore important that these
methods are thread safe.
The easiest way to guarantee that the code is
thread safe is to avoid instance variables
altogether and instead use synchronized blocks.
Simple Counter Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
package servlet_tutorials.PhoneBook;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.net.*;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
if (req.getParameter("LastName").length() > 0)
lastname = req.getParameter("LastName");
else lastname = null;
package servlet_tutorials.PhoneBook;
package servlet_tutorials.PhoneBook; import java.io.*;
import java.io.*; import java.net.*;
import java.sql.*;
import java.net.*; import java.util.*;
import java.sql.*;
import java.util.*; /**
* Re-usable database connection class
*/
public class PhoneBookBean { public class ConnectDB {
private Connection con = null; // setup connection values to the database
private Statement stmt = null; static final String DB_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
static final String URL = "jdbc:odbc:PhoneBook";
private ResultSet rs = null; static final String USERNAME = "anon_user";
private String query = null; static final String PASSWORD = "";