Servlets and Tomcat
Servlet – Java technology-based Web component, managed by a container, that generates dynamic content. Servlets interact with Web clients via a request/response paradigm implemented by the servlet container.
To be a Servlet, a Java class just has to implement the Servlet interface. A full implementation would be Tomcat or Websphere. The Servlet 2.4 API is both a specification and a set of Java classes. It’s also an API for JSP. Tomcat was developed by Sun to work with Servlets. Versions of Tomcat are in sync with the versions of Servlet. Latest version of Tomcat is Tomcat 7 but these notes will focus on Tomcat 6.
Catalina = Tomcat Servlet Engine, module that handles servlets
Jasper = Tomcat JSP Engine, module that handles JSPs
Coyote = HTTP connector component, set of code that allows you to put a web server in front of Tomcat
- Apache works better as a front end for web requests
- Passing on to Tomcat is done by Coyote
Will see these referenced in the log files. Tomcat was donated by Sun to the Apache project. Would install Apache and Tomcat, along with the connector (Coyote). Most use a more robust web server like Weblogic or Websphere as a front end instead.
Rules for Writing Java Servlet Code
Never write a main method because you are not writing the starting point of your application.
Communicating over the Web
Browser -> WEB -> Server
Email protocol (SMTP)
FTP protocol (FTP)
Web protocol (HTTP)
Protocols allow any browser to communicate with any server. Address will locate the server. Incoming request will request a particular application. When it finishes, it will send a response back to the browser.
Packets are made to be extremely simple and small. Normally, sent as ASCII text. To protect from packet sniffing, we have to encrypt the text files.
The browser sends a querystring to the server containing text in the address line. The response can only send certain types of information. Each response file is of one datatype and it has to be represented by ASCII text. Information is given after the ? on the address line.
+ – Replacement for Spaces
& – Delimiter
HTTP Request / Response Formats
Action = “http://www.amazon.com/radios/main.jsp” – Specifies the resource to be accessed
Method = GET or POST – Tells how you want the data to be sent
Gives info about the request
POST does not post the info into the query string. Instead the info is embedded in the request and does not appear in the address line. Always use POST in the form. Everything is Header information until you hit the blank line. Cannot encrypt data in the address bar, but you can encrypt the posted form request.
Response is the same, regardless of request. Response includes:
- Repeats the protocol version – HTTP/1.1
- Return Code – 200, 404
- Return Message – OK, Resource Not Found
- Set-Cookie: JSESSIONID=rb8huhg6t6; path=/testEL
- Content-Type: text/html (or application/ms-word, image/gif)
- Content-Length: 397 (how many bytes are in the body)
- Date: 10 May 2011 03:30:40 GMT
- Server: Apache-Coyote
- Connection: close
Can use a variety of languages.
- C / C++
- Java (Only possible by using Servlet as the receiving resource)
This class is about Java, so that’s what we’ll concentrate on. The Servlet will receive the Request and send back the Response. Whatever you do with a Servlet, you can do with a JSP. Each has its own set of functions that it does well.
Maintainability and Extensibility
MVC Architecture built on the principle that we’ll use both Servlets and JSPs. JSPs were initially developed as an answer to Microsoft’s ASP. It was intended as a replacement for Servlets; however , Servlets still handle some things better. JSPs was a tag language that could be done easily. Servlets was left to the hard-core Java programmers.
Uses a Java Application Server
Sun has to certify it as a Java Application Server (i.e. Weblogic, JRun)
Tomcat is a partial Java Application Server because it only understands Servlets and JSPs. Tomcat does not understand EJBs or Java Messaging. Tomcat was written to follow the Servlet and JSP Specs, so you can take the same app and put it on Weblogic.
“Write once, run anywhere”
Written in Java. We install our Java applications into Tomcat. Placeholder for the different application within a Java application. Main method is started in Tomcat. Anything that you write is added to the Tomcat application and will be managed by Tomcat. Think of Tomcat as an “Implementation of the Servlet Spec”. Have to have agreements with Tomcat
Who is going to communicate and when?
1) Java Classes
- XML Files
Java code implements those interfaces exposed by Tomcat. Also have to have an agreement on architecture
2) Java Web Application Structure
To work, the Java application has to include certain directory structures and required files. When you fire up Tomcat, it checks each application. If it is wrong, Tomcat will ignore your application like it is not there. 80% of your project is finished because it uses Tomcat. Tomcat comes with all the source code, so you can edit and recompile it.
- Install, Run, and Administer Tomcat
- Understand the Servlet Specification
Tomcat Installation Steps
- Java 1.5+ on your PC
- Set JAVA_HOME environmental variable to your top-level Java directory (not bin)
- Program FilesJavajdk1.6.0_11
- Add the Java ‘bin’ directory to your PATH variable
- Unzip tomcat to the C Drive. Install to the root. Space in Program Files can cause problems.
Additional Steps for the Development Environment
6. Create or modify the CLASSPATH environmental variable. Set it to include C:tomcat6libservlet-api.jar
7. Uncomment the following
7. Add the following attributes to the Context tag in context.xml (also in the conf directory)
<Context reloadable=”true” privileged=”true”>
- Use temporarily when we are in development. In production, this would add a lot of overhead comparing dates.
localhost – Special identifier for the local machine
Server address will get you to the apartment complex. Port number is like the apartment number
1433 – SQL Server
1521 – Oracle
25 – Mail
21 – FTP
Tomcat runs on port 8080, so we specify 8080 for our Tomcat install
Have Tomcat running in the foreground. Don’t run it as a service. Errors will show up on the Tomcat console. Everything in Tomcat is self-contained in the folder.
Tomcat Directory Structure (in order of importance)
– lib (Lots of jar Files that is used internally)
– conf (Most important files are server.xml and web.xml)
– webapps (Root directory for all web applications)
Application Directory Structure
- Class files in package structure (servlets go here)
- Classes in jar files
Have to build a web application to access your servlet. The name of the folder is the name of the webapp. Tomcat will automatically run ROOT, unless you specify another application in the address. Files under the WEB-INF directory is invisible outside the server and inaccessible by URL. Also, never, ever write a Java class without a package.
HTML 4 Visual QuickStart by Castro (Peachpit Press)
HTML & xHTML – The Definitive Guide by Musciano & Kennedy (O’Reilly Press)
Head First HTML by Freeman & Freeman (O’Reilly Press)
Professional Tomcat 6 by Chopra,Li and Genender (Wrox)
Pro Apache Tomcat 6 by Moodie (Apress)
How Tomcat Works by Kurniawan and Deck (BrainySofware)