Servlet之Core Servlet

/*1、Web Programming Model*/
In software engineering,a Web application is an application that is accessed via web over a network such as the internet or an intranet.
The web programming model is made up of the following three factors:
Web Client
A software program used to access web pages.Sometimes the same as a Web Browser.
Web Server
A server is a computer that delivers services or information to other computers.In web terms:A server that delivers web content to web browers;
Communication Protocol:
A standard(language and a set of rules)to allow computers to interact in a standard way.HTTP is used as ths protocol in the web.
Contents generated by the server may both static and dynamic:
Static Contents
static contents are pre-created and stored on the server,they are the same for each web client;
Dynamic Contents
dynamic contents are contents generated by some programs on the server.The program can be CGI,Servlets and so on.To run such programs.the server must provide some add-in program called contrainer.

/*2、Install and configure Tomcat*/
Dowload Tomcat
you can download tomcat from http://tomcat.apache.org/ after download.unzip it to some directory on the disk.
Install JDK
JDK must be installed when building web application using Java.You can download it from http://java.sun.com
Setup environment variables
JAVA_HOME=root directory of JDK
CATALINA_HOME= root directory of tomcat
Verify server setup
After server startup,visit the following address http://127.0.0.1:8080 or http://localhost:8080

/*3、Struture of web application*/
Web application
A Web application is a collection of servlets.HTML pages,classes and other resources that make up a complete application on a Web server.The Web application can be bundled and run on multiple containers from muliple vendors.
Directory Structure
webroot--public --sub folders
--WEB-INF--web.xml
--classes
--lib
webroot
a user defined name of the web application
public
user defined folder or file names,it together with its subfolders are public to web clients.
WEB-INF
used by web server,not public to web clients.
classes
directory for servlets and utility classes.
lib
jars containing servlets and utility classes.
web.xml
The Web application deployment descriptor.Servlets,Filters and Listeners are deployed by adding configuration entries in the file.
Deploy web application in tomcat
to deploy a web application in tomcat,one should put a pre-created web application structure under the directory of $CATALINA_HOME/webapps
Visit the Web application:
http://serverip:port/webroot/resourcepath

/*4、Steps to develop Servlets*/
Steps to develop Servlets:
1.Write Servlet class and other classes it uses.
2.Compile Servlet and put class files to the classes directory of some web application.
3.Deploy Servlet by adding configration entires in web.xml
4.Restart web server and test servlet.

1.Write Servlet class and other classes it uses.
All java Servlets must implement javax.servlet.Servlet interface and provide lifecycle methods and other methods.these methods include:
init
service
destory
getServletConfig
getServletInfo
The init,service,destory mothod are called by the Container.They are lifecycle methods.
//MyServlet.java
package com.chenzw.testServlet;
import javax.servlet.*;
import java.io.*;
public class MyServlet implements Servlet{
private ServletConfig config;
public void init(ServletConfig config)throws ServletException{
this.config=config;
}
public ServletConfig getServletConfig(){
return config;
}
public void destory(){
}
public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException{
PrintWriter out=response.getWriter();
out.println("Hello Servlet!!");
out.close();
}
public String getServletInfo{
return "It's chenzw's Servlet!";
}
}
2.Compile Servlet and put class files to the classes directory of some web application.
To compile Servlets,you must get Servlet depended class files and make it available to your compilation environment.the class files can be found at:$CATALINA_HOME/common/lib/servlet-api.jar
3.Deploy Servlet by adding configuration entires in web.xml
A servlet should be configured to serve some url,so as to let web clients visit it.this is done by adding configuration entries in web.xml.
//Configuration entries in web.xml
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.chenzw.testServlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/mySerlvet<url-pattern>
</servlet-mapping>
4.Restart web server and test servlet
After restart web server,visit the following address using your web browers:http://localhost:port/myServlet
What if you develop more servlets?
public class MyServlet extends GenericServlet{
public void service(ServletRequest resquest,ServletResponse response)throws ServletExcetpion,IOException{}
}
What if you deal differently with HTTP GET and POST request?
public class MyServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{}
}

你可能感兴趣的:(servlet)