JSP and servlet Knowledge points summary. __js

Source: Internet
Author: User
Tags button type

A The difference between a JSP and a servlet:
1.jsp is compiled into a servlet. (JSP is the essence of SERVLET,JVM can only identify Java classes, not recognize the JSP code, the Web container to the JSP code into the Java class can be recognized)
The 2.jsp is better at showing on the page, and the servlet is better at logic control.
There are no built-in objects in 3.Servlet, and built-in objects in the JSP must be obtained by HttpServletRequest objects, HttpServletResponse objects, and HttpServlet objects.
JSP is a simplification of the servlet, using JSP only to complete the programmer needs to output to the client content, JSP Java script How to mosaic into a class, completed by the JSP container. The servlet is a complete Java class, and the service method of this class is used to generate a response to the client.

Teacher's Answer 1:
JSP is essentially a servlet, but the two are created differently. A servlet is a completely Java program code that is adept at process control and transaction processing through the servlet
To generate dynamic Web pages; JSP is composed of HTML code and JSP tag, can easily write dynamic Web pages
In practice, the servlet is used to control the business process, and JSP is used to generate dynamic Web pages. In the struts framework, the JSP is in the view layer of the MVC design pattern, and the servlet is at the control layer.
Answer 2:
JSP is an extension of the servlet technology, which is essentially a simple way of servlet. JSP is compiled "class servlet". The main difference between servlet and JSP is that the application logic of the servlet is in the Java file and is completely detached from the HTML in the presentation layer. In the case of JSP, Java and HTML can be combined into a file with a. jsp extension. The JSP focuses on the view, and the servlet is used primarily for control logic.

Two The nine objects of JSP, seven big movements, three orders

JSP nine large built-in objects:
1>out output data to the client, byte throttling. such as Out.print ("Dgaweyr");

2>request receives the HTTP request from the client.
String GetParameter (string name): Gets the value of the name of the form parameter.
String[] Getparametervalues (String name):(get string[] check box is often used.
SetAttribute (String name,object obj): Sets the property name to name, and the property value is obj.
GetAttribute (String name), get property value.

3>response: Encapsulates the response generated by the JSP and sends it to the client to respond to the customer's request. redirect jumps to any interface. (Server jump)
Addcookie (Cookie cookie):
Sendredirect ("/wel.jsp"): Jump to the specified page

4>session: For saving user information, tracking user behavior, in the currently open browser, multiple pages share data. The session object refers to a client-server conversation that starts with a webapplication from the client to the server until the client disconnects from the server. It is an instance of the HttpSession class.
SetAttribute (String name,object obj): Sets the property name to name, and the property value is obj.
GetAttribute (String name): Gets the property value.

5>application object: Enables the sharing of data among users, and holds global variables. It starts at the start of the server until the server shuts down, during which time this object will always exist; so that the same property of this object can be manipulated between the user's front and back connections or between different users The operation of this object's properties anywhere will affect access by other users. The startup and shutdown of the server determines the life of the Application object. It is an instance of the ServletContext class.

The difference between session,application,request:
In a project, try to use as few sessions as possible, because too much session will affect the execution efficiency of the program. It is mainly used to save login information (user information, permissions, resources) that is frequently used information.
Application: For multiple browsers to share data, multiple users share the object, you can do counters. It is used exactly the same as the session.
Data range:
Application (invalidated when server shuts down) >session (expires when browser shuts down) >request (only between two jump pages)

The 6>page object represents the JSP entity itself, that is, the current page is valid. Equivalent to this in Java.
Data range: Page<session<application

7>.exception: Represents an exception at run time.
Add an instruction to the page where the exception will occur: <%@ page errorpage= "Handle the wrong pages. JSP"%>
Writing on the page that handles the exception: <%@ page iserrorpage= "true"%>

The 8>.pagecontext object PageContext object provides access to all objects and namespaces within the JSP page, which means that he can access the session where the page is located, as well as a property value of the application that is located on the site. He is equivalent to all the features of the page synthesizer, its name is also called PageContext.

9>.config jsp corresponding to the servlet configuration, you can get web.xml in the first-order parameters.

JSP Seven big gestures:
One: Include dynamic inclusion (compiled separately):
Implement <jsp:include page= "included.jsp" flush= "true" with Jsp:include action/>
It always checks for changes in the contained files, suitable for inclusion of dynamic pages, and can take parameters. Flush property: True to indicate that the page can be refreshed. Default to FALSE;

Two: Usebean action (the second way the JSP page uses JavaBean):
<jsp:usebean id= "Object name class=" package name. Class name "scope=" Scope (request/page/application/session)/>
The scope defaults to page (valid on this page).
Three: GetProperty action (name is the ID in the Usebean action).
To remove an attribute value from an object: <jsp:getproperty name= "JavaBean object" property= "JavaBean object property name"/>
Four: SetProperty action (name is the ID in the Usebean action):
Set property value for object: <jsp:setproperty name= "JavaBean object" property= "JavaBean object property name" value=http://www.hake.cc/kf/201109/"value"/ >
Set property value for object: <jsp:setproperty property= "JavaBean object property name" Name= "JavaBean object" param= "username"/>
(param= "username" is equivalent to Value=http://www.hake.cc/kf/201109/<%=request.getparameter ("username")%>)

V: Param action (transfer parameters)):
To reach the jump page you can remove the value of the parameter by Request.getparameter ("parameter name")
<jsp:include page= "URL to page" >
<jsp:param name= "argument name 1" value=http://www.hake.cc/kf/201109/"parameter value 1" >

<jsp:param name= "argument Name 2" value=http://www.hake.cc/kf/201109/"parameter value 2" >
...........
</jsp:include>
Or:
<jsp:forward page= "URL to page" >
<jsp:param name= "argument name 1" value=http://www.hake.cc/kf/201109/"parameter value 1" >
<jsp:param name= "argument Name 2" value=http://www.hake.cc/kf/201109/"parameter value 2" >
...........
</jsp:forward>

VI: Forward action:
Jump page: <jsp:forward page= "login.jsp"/>

Seven: plugin action: &LT;JSP:PLUGIN&GT;: Used to specify plug-ins to run on the client

JSP three major directives:
One: page instruction:
1. Specify page encoding. Example:
<%@ page language= "java" contenttype= "TEXT/HTML;CHARSET=GBK" pageencoding= "GBK"%>
2. Import Package, Example:
<%@ page import= "java.util.*,java.text.*"%&GT;.
II: Include directives
Static inclusion (Unified Compilation): <%@ include file= "included.jsp"%>

Three: Taglib

Three Static inclusion and dynamic inclusion in JSP
Dynamic include (<jsp:include page= "included.jsp"/>)
Static include (<%@ include file= "included.jsp"%>)
1. Static include results are the other JSP into the current JSP, the two are integrated, you can achieve the sharing of data can be said to be unified compiled,and
The dynamic include structure is both independent, which is compiled separately until the output is merged.
2. Dynamic include JSP file independence is very strong, is a separate JSP file, need to use the object, page settings, are created by themselves, and static include is simply the code written in the outside of a shared method, all variables are can and include its main file share, The two are highly integrated and cannot have conflicting names of variables. Page settings can also be borrowed from the master file.
3. Dynamic inclusion always checks for changes to included pages, static inclusions may not necessarily check the changes contained in the page.
4. Dynamic inclusion with parameters, static contains can not take parameters. such as (<jsp:include page= "included.jsp" > Put parameters </jsp:include>);

Fourthe life cycle of the servlet
Loading(When the server starts, it will go to the Web.xml file to locate the servlet file configuration and create an instance of the servlet) →Initialization of(init () This method executes only once) →Perform(Service (), Doget (), doPost ()) →Destroy(Destruction of Destory ())
Service (): The method itself contains doget () and DoPost (). If the server discovers the service () method, the Doget () is no longer executed, doPost ().
It is generally not recommended to override the service method of the parent class. Because this method has been overridden Doget methods and Dopost methods will not be exploited.
There is no service () method that executes the Doget () method by default.

Five Top Ten ways to Servlet
Init (httpservletconfig config): Called by the servlet container to indicate that a servlet was put into service.
Service (HttpServletRequest request, httpservletresponse response): Called by the servlet container to allow the servlet to respond to a request
Doget (HttpServletRequest request, httpservletresponse Response): The server invokes a GET request that handles the client.
DoPost (HttpServletRequest request, httpservletresponse response): Called by the server to process the client's post request.
DoPut (HttpServletRequest request, httpservletresponse response): Called by the server to handle the client's put request.
Dodelete (HttpServletRequest request, httpservletresponse response): Called by the server to process the client's delete request.
Destroy (): Called by the servlet container to inform a servlet that it was excluded from the service.
Getservletinfo (): Returns information about the servlet, such as author, version, copyright.
Getservletconfig (): Return ServletConfig Object
Dohead (HttpServletRequest request, httpservletresponse response): Called by the server to process the client's head request.

Six The basic architecture of the servlet class
Servlet interface: Primarily defines the lifecycle method of the servlet
ServletConfig interface: Provides a servlet with several important objects and methods for using container services.
ServletContext interface: is a servlet context object that was created at server startup and provides the servlet with several important methods for using container services.
Genericservlet Abstract class: Provides a general implementation of the servlet (including the implementation of the servlet and ServletConfig two interfaces), An important object of a servletconfig type that the container passes through the Init method to the servlet is saved.
HttpServlet Abstract class: Provides a general implementation for the servlet that handles HTTP requests, primarily by defining and implementing several service methods.
Inheritance relationship: Genericservlet inherits the servlet and ServletConfig interfaces; HttpServlet inherits Genericservlet; The servlet we wrote Inherits HttpServlet.

Seven How to configure a servlet to make it run
1.servlet's name, package class
<servlet>
<servlet-name>httpservletdemo (name for servlet) </servlet-name>
<servlet-class>com.lxitedu.httpservletdemo (package name. Class name) </servlet-class>
</servlet>

2. Map. Address (URL) of the access
<servlet-mapping>
<servlet-name>httpservletdemo (exactly the same as the servlet name above) </servlet-name>
<url-pattern>/myservlet (file name displayed at run time Address bar) </url-pattern>
</servlet-mapping>

Eight. What's the difference between get and post
1.post is safer than get. (The question mark method of get is used to pass the value)
2.post receives a longer length.
3.post submits the request as a form, and the get is submitted as a browser. So get more quickly. 4.post is more able to solve our Chinese garbled problem.
A GET request is by default, and the method property of form, if not specified, defaults to a GET request.
Get requests, which display the parameters in the browser's address bar, are displayed in the format, after the address starts with a ' & ' that separates the parameters, and can be obtained by HttpServletRequest () object's GetQueryString () method to obtain the parameter values of the getting request.
Post request: The getInputStream () method of the ServletRequest object can obtain an input stream from the socket, which can be used to implement the file upload. The Getreader () method can directly get the parameters of the POST request

Nine. Say what kinds of jumps are in the JSP or servlet. What's the difference between them?
1. Client Jump:
1. Use the Action property of the form form to set the page to jump (this method can pass the form property value of this page to the next page):
<form action= "my.jsp" Name= "Lili" onsubmit= "return Test ()" >
<input type= "Submit" value=http://www.hake.cc/kf/201109/"submitted" >
or (you need to manually submit the form for the button type):
<input type= "button" value=http://www.hake.cc/kf/201109/"register" onclick= "Test ()" >
Functions for javascript:
function Test () {
Set the jump page manually
Document.lili.action= "my1.jsp";
Document.lili.submit ();
}
2. Use a page jump method in JavaScript (you cannot pass property values from form forms to the next page):
window.location.href=http://www.hake.cc/kf/201109/"my.jsp";
Window.location.replace ("my.jsq");//This method cannot back up the page.
2. The service side jumps two kinds of methods comparison:
<jsp:forward page= "xxx.jsp"/&GT;: equivalent to
Request.getrequestdispatcher ("xxx.jsp"). Forward (Request,response);
This server's resources jump, more efficient. The Address bar does not change (still the page before the jump). The Request property value can be obtained.
Response.sendredirect ("xxx.jsp"):
Redirect to any resource. The address bar is changed to the current page. Cannot get the Request property value.

10. Can the data in the servlet be shared with the JSP? If you can pass
can, we can put the data in the servlet into the Httpsession,httpservletrequest,servletcontext,pagecontext object,
Use the corresponding built-in object in the JSP page to get the data in our servlet.

Related Article

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.