Servlet and JSP Summary

Source: Internet
Author: User
Tags html comment

(i) Servlet

1. Deploying servlet containers
1) Install JDK, configure java_home< root path >; extract Tomcat
2) Add the Tomcat script path under path < open in any directory, configure catalina_home< root path >
3) Modify the port number through the Server.xml under the Tomcat configuration file <port= "8080" >
4) Modify the Tomcat Manager user name password by conf tomcat-users.xml
<tomcat-users>
<role rolename= "Manager-gui"/>
<user username= "Tomcat" password= "S3cret" roles= "Manager-gui"/>
</tomcat-users>
2. Java Web directory structure
-WebContent
-Web-inf
-Classes
-Lib
-Web. xml
-JSP Page
-HTML page
-Pictures ...
1) Eclipse modifies the default output directory of the compiled file, set to Classes<build path-to configuration build path-->source>
2) Create catalina/localhost/test.xml< in the Conf directory of Tomcat to configure Web applications under any directory >
<context
Path= "/wuyong"
Docbase= "C:\\users\\administrator\\workspace\\test_3\\webcontent"
Reloadable= "true"/>
! Path name as the configuration name under manager
3. Developing Java Web projects with eclipse
1) Customizing new perspective <windows-->perspective-->customize perspective>
2) servlet container management servlet lifecycle
Load: Specifies the order in which instances are created <load-on-startup>
Constructor: Creating a Servlet instance
Init ()
Service ()
Destory ()
4, the ServletConfig interface corresponding method
Getinitparameter ();
Getinitparametername ();
Servletname ();
ServletContext ();
1) Get the absolute path to a file on the server for the current web app, not the pre-deployment path
Getrealpath (String path);
2) Get the name of the current web App
Getcontextpaht ();

As a computer worker, you should assemble your own computer to save economy and improve development efficiency.
5. ServletContext interface
Set initialization parameters:< Global >
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
<context-param>
Set servlet initialization Parameters:< Local, servlet load-on-startup before >
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
6. Get request and POST request
Get request data added in the Address bar, the amount of data associated with the URL address, general 4kb-7kb
Post request data encapsulated in form data, unlimited data volume
7. Service ()
ServletRequest: Encapsulates the request information
1) Get Request parameters
String GetParameter (string name): Returns a parameter value based on the request parameter name
String Getparametervalues (string name): Returns a string array of parameters corresponding to the request parameter name
Enumeration Getparameternames (): Returns the enumeration object corresponding to the parameter
Map Getparametermap (): Returns the key value pair key--> parameter name value--> argument value, String type
2) get the request URL
3) How to get the request
4) If GET request, get URL? After the data
5) Get the mapping path for the servlet
6) Several methods related to attribute:
Servletresponse: Encapsulates the response information
1) *getwriter (): Returns the PrintWriter object. Call the print () method of the object to print the printing parameters to the browser
2) Set Response type: Response.setcontenttype ("Application/msword")
3) void Sendredirect (String location): The specified redirect. (This method is defined in HttpServletResponse)
8, the implementation of the Servlet interface class
genericservlet< not used >
> of Httpservlet

(ii) JSP
1, JSP definition: Full name Java Server Pages,java Server page. It is fundamentally a simplified servlet design.
2, nine hidden objects of JSP < No declaration can be used directly >
Request
Response
PageContext
Session
Application
Config
Out
Page
exception< Special: If the property of the page tag is set-->iserrorpage= "true" >
3.2 Ways to change lines
①%>
<br>
<%
②out.print ("<br>");
4. Scope attribute range from small to large
Pagecontext,request,session,application
5. JSP syntax
1) static HTML content is called
2) JSP expression <%=%>
3) script fragments, Java code fragments of the trap HTML code in Java code, multiple script fragments can be accessed by each other
4) JSP Declaration <%! %>
5) JSP comment <%----%> HTML comment <!---->
6. Property manipulation of Domain objects
Domain object:
PageContext: The scope of the property is limited to the current JSP page
Request: The scope of the property is limited to the same request
Session: The scope of the property is limited to one conversation: The browser opens until it is closed as a session < session does not expire during this period >
Application: The attribute is scoped to the current Web application. As long as the property is set in one place, the JSP or servlet is available elsewhere, and the server shutdown or restart disappears
7. Request redirection and Request forwarding
Creation of servlet classes, selectable mappings and methods
Request forwarding and redirection:
1) Essential Difference: the forwarding of the request only makes one request, while the redirect makes two requests
Specific:
① forwarding of requests: Address bar is the address where the initial request is made
REDIRECT requested: The Address bar is no longer the initial request address, the address bar is the address of the last response
② Request Forwarding: In the final servlet, the request object and the request object in transit are the same
Requested redirection: In the final servlet, the request object and the request object in transit are not the same object
③ Request Forwarding: A resource that can only be forwarded to the current web App
REDIRECT requested: Can redirect to any resource
④ Request Forwarding:/represents the root directory of the current WEB app
Requested redirect:/represents the root directory of the current WEB site
RequestDispatcher
Requestrediect
8, JSP 2.0 of three kinds of instructions Page,include,taglib
Page directive:
1) Global any position is globally valid, it is recommended to put it in the starting position
2) Common Properties of the page directive:
①import Property: Specifies the class to be imported by the servlet corresponding to the current JSP page
<%@ page import= "Com.ants.test.Test"%>
②session property: Evaluates to TRUE or FALSE to specify whether the session hidden variable of the current page is available
<%@ page session= "false"%>
③errorpage and Iserrorpage
> errorpage Specifies the page that is actually responding if the current page error,/represents the current WEB app root, request is forwarded
<%@ page errorpage= "/error.jsp"%>
> iserrorpage Specifies whether the current page is an error handling page, if the property value is true, you can use the implied object exception, which is not recommended to have direct access to the page, and the property value defaults to False
> Web-inf files are private files that cannot be accessed directly through a URL, but can be accessed via request forwarding
<%@ page errorpage= "/web-inf/error.jsp"%>
④contenttype Property: Specifies the type of response for the current page, which is actually called Response.setcontenttype ("Text/html;charset=utf-8")
⑤pageencoding Property: Specifies the character encoding of the current JSP page. Generally consistent with CharSet in ContentType
⑥iselignored Property: Specifies whether the current page can use an El expression, and usually evaluates to True
Include directive: The other file is introduced statically into the servlet source file, where the file property is used to specify the relative path of the referenced
Start with the WEB app's root directory
<%@ include file= "Registerurl"%>
9. JSP Tag
The tag name is prefixed with a lowercase jsp, such as <jsp:include>, <jsp:forward>
<jsp:include> dynamically introduces additional file resources to generate 2 servlet source files
<jsp:include page= "b.jsp" ></jsp:include>
<jsp:forward>
<jsp:forward page= "/include/b.jsp" ></jsp:forward>
10, Chinese garbled problem
1) JSP, development tools, browser encoding format changed to Utf-8
2) If GET request-->servlet container conf/server.xml Modify encoding format
If the POST request is added to the service () in the-->servlet interface < prerequisite extends HttpServlet implementation class >
Request.setcharacterencoding ("Utf-8")
Response.setcharacterencoding ("Utf-8")
11. MVC Design Pattern

Servlet and JSP Summary

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.