1:note.txt
1: Developing Dynamic Web Engineering (Java Web project) with Java EE version of Eclipse 1) switch development options to Java EE
2) You can find the package Explore in Window->show view and drag it to the left side of the development zone
3) Create a new Tomcat server in the servers panel
4) New Dynamic Web project, where target runtime needs to select Tomcat
5) Developing Java Web applications
6) You can run the Web project through run on server
2:servlet's HelloWorld
1) Create a servlet interface implementation class
2) Configuring and mapping Servlets in the Web. xml file
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
3) Browser access servlet via <url-pattern>/hello</url-pattern>
3:servlet Container: Running Servlet,jsp,filter,listener
1) can be used to create a servlet and invoke the servlet's related life cycle method
2) Jsp,filter,listen,tag ... Running in a servlet container
4:servlet Life cycle Methods: The following methods are called by the servlet container
1) Constructor: When requesting a servlet for the first time, create an instance of the servlet, call the constructor (description is a single instance of servlet)
2) Init: Called only once, invoked immediately after the servlet container is instantiated, used to initialize the servlet
3) Service: Called multiple times, the service method is invoked each time it is called, and is actually used in response to the request
4) Destory: Called when the servlet container is destroyed, only once, releasing the resources currently occupied by the servlet
5:load-on-startup: Creating an instance when it is loaded
6: About Servlet-mapping
1) The same servlet can be mapped to multiple URLs, that is, the settings of the <servlet-name> child elements of multiple <servlet-mapping> elements can be the same servlet's registered name
2) The servlet can also be mapped to a URL using * (wildcard characters), but there can be only 2 fixed formats
One is: *. Extension (html,jsp), the other is:/*
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The following are not legal:
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/*.html</url-pattern>
</servlet-mapping>
7:servletconfig: is an interface that encapsulates the servlet configuration information and can get the ServletContext object
1) Configuring servlet initialization parameters
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123</param-value>
</init-param>
2): Get initialization parameter: Getinitparameter (String name): Gets the initialization parameter for the specified parameter name
Getinitparametername (); Gets the enumeration object consisting of the name of the parameter
String user=servletconfig.getinitparameter ("user");
String password=servletconfig.getinitparameter ("password");
System.out.println ("User:" +user+ ", Password:" +password);
Enumeration<string> Names=servletconfig.getinitparameternames ();
while (Names.hasmoreelements ()) {
String name=names.nextelement ();
String Value=servletconfig.getinitparameter (name);
System.out.println ("^^" +name+ ":" +value);
}
3): ServletContext: Is an interface (servlet context)
The Sevlet engine creates a corresponding ServletContext object for each Web application, contained in the ServletConfig object
Call the Servletconfig.getservletcontext method to return a reference that corresponds to ServletContext
--Because each servlet Web application shares a ServletContext object, ServletContext is a application (APP): a common data message for all users
① getting initialization parameters for the current Web application
Set initialization parameters: ServletContext can be obtained for all servlets, while the servlet initialization parameters can only be obtained by a specified servlet
<!--Configure initialization parameters for the current Web application--
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql://localhost/testjdbc</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
Method:
ServletContext Servletcontext=servletconfig.getservletcontext ();
String driver=servletcontext.getinitparameter ("Driver");
String jdbcurl=servletcontext.getinitparameter ("Jdbcurl");
String user=servletcontext.getinitparameter ("user");
String passwords=servletcontext.getinitparameter ("password");
System.out.println ("Driver:" +driver);
System.out.println ("Jdbcurl:" +jdbcurl);
System.out.println ("User:" +user);
System.out.println ("Passwords:" +passwords);
System.out.println ("---------------------------\ n");
Enumeration<string> Names2=servletcontext.getinitparameternames ();
while (Names2.hasmoreelements ()) {
String name2=names2.nextelement ();
String Value=servletcontext.getinitparameter (name2);
System.out.println (name2+ "-to" +value);
}
② gets the absolute path of one file for the current web app: Getrealpath () method, not the path before file deployment
③ get the root directory name of the current web app:
String Contextpath=servletcontext.getcontextpath ();
System.out.println ("Web root directory Name:" +contextpath);
④ gets the input stream for a file of the current web App
getResourceAsStream (String path):p ath/equivalent to the current Web application's path
InputStream Isinputstream2=servletcontext.getresourceasstream ("/web-inf/classes/jdbc.properties");
System.out.println ("2.") +ISINPUTSTREAM2);
Several methods related to attribute: get
8: Use get differs from POST request:
GET request: Parameter appended to URL? With key-value pairs present, sending data using GET Requests is typically limited to 1kb (hyperlinks, which are requested directly in the browser)
POST request: Send the form content as an HTTP message to the Web server with a larger amount of data transferred than the GET Request data (file upload must be a POST request)
9: How to Get request information in the servlet
1) The Servlet's service method is used in response to the request: Because the service method is called every time the request
protected void Service (HttpServletRequest request, httpservletresponse response)
HttpServletRequest Request: Encapsulates the requested information and can obtain any request information from it
HttpServletResponse response: Encapsulates the corresponding information, what to respond to, call the implementation of the interface method
①: Get Request Parameters:
String GetParameter (string name)//Gets the value of the specified parameter name
Map Getparametermap (); Gets the name of all request parameters and the corresponding value
Enumeration Getparameternames () Gets the name of all request Parameters//enumeration is an iterator, which is a version before iterator appears
String[] Getparametervalues (String name) gets a set of values for the specified parameter name (check box)
② Get url:servletrequest Sub-interface HttpServletRequest
③
2:login.html
<! DOCTYPE html>
<meta charset= "UTF-8";
<title>insert title here </title>
<body>
<form action= "Loginservlet" method= "post";
username:<input type= "text" name= "user"/><BR/>
password:< Input type= "password" name= "password"/><BR/>
interesting:<input type= "checkbox" Name= "Interesting" value= "listening"/>listening
<input type= "checkbox" Name= "Interesting" value= "Speaking"/>speaking
<input type= "checkbox" Name= "Interesting" value= "Reading"/>reading
<input type= "checkbox" Name= "Interesting" value= "Writing"/>WRITING<BR/>
<input type= "Submit" value= "Submission"/>
</form>
</body>
3:web.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi: schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id= " webapp_id "version=" 3.1 ">
<display-name>Servlet1</display-name>
<!--Configure initialization parameters for the current Web application--
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql://localhost/testjdbc</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
<!--configuration and mapping servlet-->
<servlet>
<!--servlet Registration name--
<servlet-name>helloServlet</servlet-name>
<!--servlet Full class name--
<servlet-class>com.test.HelloServlet</servlet-class>
<!--configuring servlet initialization Parameters--
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.test.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<!--need to be consistent with the Servlet-name child nodes of one of the servlet nodes-
<servlet-name>helloServlet</servlet-name>
<!--mapping specific access path:/Current web App's root directory--
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4:loginservlet
Package com.test;
Import java.io.IOException;
Import Java.util.Arrays;
Import java.util.Enumeration;
Import Java.util.Map;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Loginservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
}
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Doget (Request,response);
}
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
SYSTEM.OUT.PRINTLN ("request came");
String user=request.getparameter ("user");
String password=request.getparameter ("password");
System.out.println ("User name:" +user+ ", Password:" +password);
String[] Params=request.getparametervalues ("interesting");
for (String value:params) {
System.out.println ("--" +value);
}
Enumeration<string> Names=request.getparameternames ();
while (Names.hasmoreelements ())
{
String name=names.nextelement ();//Get element
String Value=request.getparameter (name);
System.out.println (name+ "-to" +value);
}
Map<string,string[]> Map=request.getparametermap ();
EntrySet (): Gets the key-value pair,
For (map.entry<string, string[]> entry:map.entrySet ()) {
Arrays.aslist: Output An array of objects as a list collection
System.out.println (Entry.getkey () +---+arrays.aslist (Entry.getvalue ()));
}
HttpServletRequest is the ServletRequest sub-interface
HttpServletRequest httpservletrequest= (httpservletrequest) request;//down transformation
String Requesturl=httpservletrequest.getrequesturi ();
System.out.println ("Requested URL" +requesturl);
String Method=httpservletrequest.getmethod ();
System.out.println ("Method of Request:" +method);
}
}