1, Servlet 2, ServletConfig 3, ServletContext 4, HttpURLConnection

Source: Internet
Author: User

1, Servlet 2, ServletConfig 3, ServletContext 4, HttpURLConnection
    • 07. Five
    • /
    • J2ee
    • /
    • No Comments

One, the first servlet's writing process
1, the establishment of Javaweb application directory
helloservlet-– App Name
web-inf--–
Classes:servlet right here.
Lib
Xml

2. Creating a servlet class in the classes directory
A servlet must implement the Javax.servlet.Servlet interface, either directly or indirectly
Generally we choose to inherit Javax.servlet.GenericServlet or Javax.servlet.http.HttpServlet class

3. Writing servlet classes
Package Com.itheima;
Import java.io.*;
Import javax.servlet.*;
First servlet
public class HelloServlet extends genericservlet{

public void Service (ServletRequest req,
Servletresponse Res)
Throws Servletexception,
java.io.ioexception{

outputting data to the client
String data = "Hello Heima";
OutputStream out = Res.getoutputstream ();
Out.write (Data.getbytes ());
}

}

4. Compiling the servlet class
Compilation needs to be Servlet-api.jar (in Tomcat's lib directory)
4.1 Setting Classpath:set classpath=%classpath%; C:\apache-tomcat-6.0.32\lib\servlet-api.jar
4.2 Because of the package name, go to the classes directory and compile with the following command
Javac-d. Helloservlet.java

5. Configuring Access Path mappings for Servlets

The configuration for the Javaweb application is in Web. XML and the following configuration is added in Web. xml
<servlet>
<servlet-name>helloservlet</servlet-name><!– Develop a servlet name –>
<servlet-class>com.itheima.helloservlet</servlet-class><!– make the corresponding servlet class, using the class's full name –>
</servlet>

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern><!– mapping path for user access –>
</servlet-mapping>
6. Visit
Http://localhost:8080/HelloServlet/Hello

Two, the servlet specification provides the default implementation of two servlet interfaces
1, Genericservlet: Generic (default) protocol-independent implementation of
2, HttpServlet: The servlet default implementation related to the HTTP protocol. The servlet we write generally needs to inherit the class only
three, when writing a servlet, it is generally necessary to inherit httpservlet.
1, do not overwrite the service method, the default implementation provides some standard content
2, should overwrite the Doxxx party, because the default implementation gave a wrong hint

Iv. use MyEclipse to create a servlet directly. The template sucks.
1, MyEclipse6.6: Open the MyEclipse installation directory and search for Servlet.java files, which are template files.
2.myeclipse8.5: Open the MyEclipse installation directory and search for Com.genuitec.eclipse.wizards_8.5.0.me201003052220.jar.
Open the templates found Servlet.java file
3, modify the Servlet.java template file (stop myeclipse, note the backup)
v. servlet mappings
You can use wildcard characters *
Two formats:
*. extension (Note * There is nothing else before)
/abc/* (note with * end, with/start)
Vi. If the requested resource cannot find a corresponding Url-pattern mapping in the application's Web. XML, it will be processed by the default servlet. The value of the
default Servlet's Url-pattern is "/". A default servlet is already in the conf/web.xml of Tomcat.
when HTML or other static resources are requested, they are actually handled by the default servlet.
Vii. life cycle of the servlet
1, servlet by default, the container creates an instance of it at first access, and resides in memory
2, to instantiate a Servlet object when the container starts, you can do the following configuration
<servlet
<servlet-name>servletdemo1</servlet-name>
<servlet-class>com.itheima.servletdemo1 </servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
< The load-on-startup> value describes the boot order

Viii. Objects of ServletConfig
1. Function: Gets the initialization information configured for the servlet.
Initialization parameters can be set for the servlet in the <servlet> element of Web. XML by <init-param>
2. The ServletConfig object is passed to the servlet by the container when it instantiates the Servlet object. The value of the configuration parameter for the servlet in Web. XML can be obtained by getinitparameter the ServletConfig object (String paramname). Or use Getinitparameternames to get all the initialization parameters
Ix. ServletContext Objects
1, Function: Javaweb application of a global object, an application has only one ServletContext object. When the app starts, the container creates the object
2. Get a reference to the ServletContext object:
2.1servletconfig.getservletcontext ()
2.2 Methods inherited from the HttpServlet class Getservletcontext () can also be obtained
3, according to the characteristics of ServletContext, this object can be regarded as the total butler of the entire Web application
4. Application
4.1 Implementing data sharing between multiple Servlets
4.2 Configuring global parameters for Web Apps
4.3 Implementing a servlet's forwarding
Features of forwarding:
1. The address of the requested address bar does not change
2. Share both request and response objects
3, before and after forwarding the content on the page will not be output
4.4ServletContext Read Resource configuration file
1. Getrealpath: Gets the absolute path of the specified relative path resource. Relative paths must start with "/"
2. Read the configuration files in the classes directory:
A, using Classloader.getresourceasstream (): Not suitable for loading a large configuration file
b, using Servletcontext.getrealpath ("/Relative path")
3. Read the configuration files in the non-classes directory
Using Servletcontext.getrealpath ("/Relative path")

Parse path

Process of Request Forwarding

Get the configuration file under Web-inf

Relative path and absolute path

Compress data

Download image

Resolving thread concurrency Issues

1, Servlet 2, ServletConfig 3, ServletContext 4, HttpURLConnection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.