Servlet Study Notes-Servlet preliminary

Source: Internet
Author: User

LAMP: A golden combination of Linux + Apache + MySql + PHP
1. tomcat preliminary
Set the environment variable JAVA_HOME: JDK directory -- otherwise, startup. bat cannot be started.
Modify
<Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>
Indicates the port number occupied by tomcat
Modify
<Context reloadable = "true">
If there is an update, it is automatically loaded.
2. Http preliminary
Install HTTPLook
Manually send requests to the server
To access network resources through http, the request must start with the following format:
GET/latest.html https/1.1
Host: www.foo.com
Content-Type: text/html
{Empty rows}
------------------ TestHTTP. java
[Java]
Public class TestHTTP {
Public static void main (String [] args) throws Exception {
Socket s = new Socket ("127.0.0.1", 8080 );
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (s. getOutputStream ()));

Bw. write ("GET/HTTP/1.1"); // GET method to connect to the server, '/' to access the files in the root directory, and 'HTTP/123' based on what protocol
Bw. newLine ();
Bw. write ("Host: 127.0.0.1: 808"); // specify the domain name under the ip address because multiple domain names may be managed under one ip address.
Bw. newLine ();
Bw. write ("Content-Type: text/html"); // specify the Type of the resource to be accessed
Bw. newLine ();
Bw. newLine (); // there must be an empty row indicating that the request is over
Bw. flush ();
BufferedReader br = new BufferedReader (new InputStreamReader (s. getInputStream ()));
String str = null;
While (str = br. readLine ())! = Null ){
System. out. println (str );
}
Bw. close ();
Br. close ();
S. close ();
}
}
----------------------
3. The first servlet
There must be a WEB-INF folder, as shown below
WEB-INF
Web. xml
Configuration file of the web app
Lib
Library files used by the web app
Classes
Store compiled servlets
Import api:
Project-right-click-properties-java build path-Libraries-Add External JARS: E: \ apache-tomcat-7.0.27-windows-x64 \ apache-tomcat-7.0.27 \ lib \ servlet-api.jar
[Java]
------------------------ HelloServlet. java
Import java. io. IOException;
Import java. io. PrintWriter;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Public class HelloServlet extends HttpServlet {
Private static final long serialVersionUID = 1L;
 
@ Override
Protected void doPost (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
PrintWriter pw = resp. getWriter ();
Pw. write ("Hello Servlet! -- DoPost ");
Pw. flush ();
Pw. close ();
}
 
@ Override
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
// Solve Chinese garbled characters
Resp. setContentType ("text/html; charset = gbk ");
PrintWriter pw = resp. getWriter ();
Pw. write ("Hello Servlet! -- DoGet ");
Pw. flush ();
Pw. close ();
}

}
 
---------------------------
Configuration steps:
Copy the compiled HelloServlet. class to WEB_INF/classes.
If this class is wrapped in eclipse, you need to create a folder under the classes folder.
For example, if the HelloServlet class is under the ls/per package, create a ls folder under the classes, create a per file under the ls folder, and store HelloServlet. class under the per folder.
Modify web. xml
[Html]
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
Version = "2.5">
<Servlet>
<Servlet-name> HelloServlet </servlet-name> // servlet name
<Servlet-class> HelloServlet </servlet-class> // Directory d of the class file, full package path. If the class is in the package, write "ls.com. HelloServlet"
</Servlet>
 
<Servlet-mapping>
<Servlet-name> HelloServlet </servlet-name> // servlet name, which must be consistent with the preceding
<Url-pattern>/hs </url-pattern> // url access address, '/' indicates "http: // 127.0.0.1: 8080/web application directory "+/hs can access this servlet
</Servlet-mapping>
</Web-app>
 
 
4. Servlet Lifecycle
From the consumer, there is only one Servlet object, and the client requests are processed in the form of multiple threads.
When the first client request calls the Servlet, It is instantiated and the Servlet class constructor is called only once.
Next, call the initialization method init () to prepare for the preparation.
When the web application exits or the definition of the class changes, call the destroy () method to handle the problem.
5. Chinese garbled characters
Must be called before the character is obtained
Response Character Processing: response. setContentType ("text/html; charset = gbk ");
Request. setCharacterEncoding ("gbk ");
Accept get request Character Processing: Modify conf -- server. xml -- modify Port location -- add URIEncoding = "GBK"
6. Why are there two init () methods?
Init ()
Init (ServletConfig config)
In order to avoid rewriting init (ServletConfig config), I forgot to call the init () method of the parent class, causing the parameter initialization error. Therefore, I wrote an empty init () method, call the empty init () method during rewriting.

 


From Mu zinsong

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.