---restore content starts---
1.Servlet Introduction
Servlet is a server-side applet written in the Java language, belonging to a CGI program, but unlike traditional CGI, it is multi-threaded and can be ported on multiple platforms.
User-defined servlet classes are generally required to inherit from the HttpServlet class.
2. servlet's basic processing process
3. Implement the first Hello world with a servlet
A. Helloservlet.java or in the classes under the Web-inf
The B.hellosevlet class must inherit from HttpServlet, and by Servlet.http find Dov documents, you can see the Doget method of execution.
C. With the response parameter in the Doget method, get to the PrintWriter object, you can call the println () function to print the hypertext content.
The HelloWorld code is as follows
Package Cn.liys.servletdemo;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class HelloServlet extends httpservlet{
Protected Voiddoget (HttpServletRequest req, HttpServletResponse resp) throws servletexception,ioexception{
PrintWriter out = Resp.getwriter ();
Out.println ("
Out.println ("
Out.println ("<title>");
Out.println ("HelloWorld");
Out.println ("</title>");
Out.println ("
Out.println ("<body>");
Out.println ("Out.println ("
Out.println ("</body>");
Out.println ("
Out.close ();
}
}
D. It should be noted that when compiling the class, it is necessary to use the development package Servlet-api.jar, which is obtained from the Tomcat/lib directory.
One is the need to configure CLASSPATH content, in addition to copy the jar package to Jre/lib/ext
Finally, under Win7 it is best to run cmd as an administrator so that Javac compilation will succeed.
E. The mapping of the servlet needs to be configured in Web. Xml.
Defining a Servlet
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>cn.liys.servletdemo.HelloServlet</servlet-class>
</servlet>
Define a path map, you can define multiple mappings servlet-maping
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello.asp</url-pattern>
</servlet-mapping>
...
<url-pattern>/hello. Liys</url-pattern>
...
4. Receive form submissions with a servlet
A. The Doget () DoPost () method needs to be implemented in the servlet, which can be queried from the Java doc.
According to the HttpServlet class located in the Javax.servlet.http package, and then go to Doget, Dopost method of use.
To print, you need to check the parameters of the response getwriter ();
The code is as follows:
Package Cn.liys.servletdemo;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Inputservlet extends httpservlet{
protected void doget (HttpServletRequest req,httpservletresponse resp)
Throws servletexception,ioexception{
String info = req.getparameter ("info");
PrintWriter out = Resp.getwriter ();
Out.println ("
Out.println ("
Out.println ("<title>");
Out.println ("HelloWorld");
Out.println ("</title>");
Out.println ("
Out.println ("<body>");
Out.println ("Out.println ("
Out.println ("</body>");
Out.println ("
Out.close ();
}
protected void DoPost (HttpServletRequest req,httpservletresponse resp)
Throws servletexception,ioexception{
This.doget (REQ,RESP);
}
}
B. It is important to note that the mappings in Web. XML will navigate the path to the directory where the form is located, so that the form can be servlet-name directly in the action, not required. /ET
The Web. XML configuration fragment is as follows:
<servlet>
<servlet-name>input</servlet-name>
<servlet-class>cn.liys.servletdemo.InputServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>input</servlet-name>
<url-pattern>/InputServlet/input</url-pattern>
</servlet-mapping>
The form reads as follows:
<title>
Servlet's Doget Dopost
</title>
<body>
<form action= "Input" method= "POST" >
Content: <input type= "text" name= "info" >
<input type= "Submit" value = "Commit" >
</form>
</body>
5.servlet life cycle
The generic servlet initiates the initialization operation at the first invocation of Init ();
If you want to invoke a servlet segment that needs to be in Web. XML when Tomcat starts <load-on-startup>1<load-on-startup>
The Destory () method is typically called when the Web container ends, or when the servlet is not called for a long time, or when it is reload.
6. Get Initialization configuration parameters
Obtained by Init with config parameter
。。。
Private String database = null;
Init (ServletConfig config) throws servletexception{
Database = Config.getinitparameter ("database");
}
。。。。
This is configured in Web. xml
<servlet>
...
<init-param>
<param-name>database<param-name>
<param-value>oil<param-value>
</init-param>
</servlet>
7. Get session, application built-in object
A. The session object can be obtained through request in Doget (Request,response).
Request.getsession ()
The login information can be verified by getting the session object.
B. Acquisition of application objects through Super.getservletcontext ()
The function is defined in the parent class Generalservlet class of HttpServlet.
Java Web Learning Note-servlet01