Jsp/java web basic knowledge Review

Source: Internet
Author: User
Tags html comment

Because java web development has not been completed for a long time, I wrote this article to briefly review the knowledge of jsp/java web.

1. server. xml in tomcat, <Context> indicates a web application Context path;
DocBase: Specifies the name of the physical folder where a Web application is located.
Path: defines the context path name of a Web application. The context path contains 1st characters, which are generally "/".
Reloadable: When this property is set to true, it enables Catalina to monitor changes to files in the WEB-INF \ classes folder and WEB-INF \ lib folder in the Web Application
2. The default virtual host of tomcat is localhost, and its default Web application publishing folder is webapps. Web applications published in this folder can be accessed directly through URLs without configuration in <Context>.
3. Servlet writes java code and webpage design together, which is not conducive to maintenance. The design and maintenance of JSP web pages are more intuitive and easier than Servlet. The JSP page is finally compiled into a Servlet program by the JSP server to run.
4. JSP adopts the "compile first and then execute" working method. When a JSP page is accessed for the first time, the Web server performs the compilation operation (multi-thread operation)
5. <% Java statements conforming to the Java language %>
6. jsp execution process:
Servlet
Request-> *. jsp-> jsp parser-> *. java-> jsdk (servlet analyzer)-> *. class-> execution-> response
7.html comment <! --> Jsp comment <% -- %>
Jsp declaration <% !... %>, The declared variables, methods, or classes are all members of the Servlet class after jsp compilation.
8. tomcat places the Java code segment marked in <%> In the try {} of the Servlet implementation class _ jspService () method. Therefore, the variable defined in <%> is a local variable, and the function scope is limited to try. If there are multiple <%> tags, the entries are translated into try {} in the order they appear.
9. jsp expression <% = expression %>. After compilation, the Servlet is: out. println (expression );
10. jsp command element <% @ command element attribute = "value" %>, which consists of three main elements: include, page, and taglib. It is mainly used by JSP containers, provides relevant page Property Information for JSP containers to instruct JSP containers to translate code correctly or perform specific operations.
Include: file inclusion. When the JSP Container translates a JSP webpage into a Servlet program, it embeds the content of the specified text file into the Servlet program and replaces the include command.
<% @ Include file = "" %> static binding
Page: Used to set attributes of the current JSP page
By default, the class that has been added to the current JSP page, including java. lang. *;, java. servlet. *;, java. servlet. jsp. *; java. servlet. http. *;
IsThreadSafe indicates whether the generated servlet is thread safe. If it is true, it indicates thread safety. The JSP Container will run the JSP page www.2cto.com in multiple threads.
11. jsp action component: the JSP action component is a tag in XML syntax format and is used to control the behavior of Web containers.
<Jsp: include> dynamic inclusion
<Jsp: forward> enables the program to jump from the current page to another target page, and the browser address bar will not change. The code after the forward Action statement cannot execute
Line (you have already taken the request to another page)
<Jsp: param>, used to transmit parameter information www.2cto.com
<Jsp: plugin>, insert an applet or Bean
<Jsp: setProperty>: Set attributes of JavaBean.
<Jsp: getProperty>: outputs the attributes of a JavaBean.
<Jsp: useBean>: Find or instantiate a JavaBean
12. jsp implicit object
Simplified program design. The JSP Specification defines commonly used eight implicit objects (implicit objects). These hidden objects do not need to be created using the new keyword on the JSP page,
It is created and managed by Servlet containers and passed to the Servlet implementation class on the JSP page.
Out (JspWriter), requeset (HttpServletRequest), response (HttpServletResponse), session (HttpSession), application (ServletContext ),
Exception (Throwable), config (ServletConfig), page (Object), pageContext (PageContext)

Out. print ()/out. println (); out. write ()? PrintWriter?

Request. getParameter ()/getParameterNames ()/getParameterValues ()/
To exchange data between two JSP/Servlet programs, you can use the request range variable (getAttribute/setAttribute)
[Forward jump: A request is passed. The hyperlink is the same as the access method used to enter the address of the page in the browser address bar. A request is sent to the link page again.]
GetRequestDispatcher ("''), forwarder, obtains the forwarder of the target resource, forwards the request and response objects of the current Servlet program to the target resource through the forwarder, and jumps to the target resource to run the program, in this way, the target resource can read the request attribute passed to the previous resource through the request object.
RemoveAttribute ()/setCharacterEncoding ()/getProtocol/getRemoteAddr ()/

Response. flushBuffer ()/setBufferSize ()/getWriter (PrintWriter)/setContentType ()/setCharacterEncoding ()/sendError ()/setHeader ()/sendRedirect ("")/
Response downloads through the file output stream

The application object is shared by all jsp/servlet programs in the same context (the same web application), and is created when the tomcat server is enabled;
Application accesses <Context-param>/getInitParameter ("")/attribute related methods configured in web. xml/log ()/

Config is often used to send initialization parameters to a Servlet program.

Exception: An exception occurs when the JSP page is running. The system generates an exception object.

The page Object indicates the "current" Servlet program object, similar to the "this" keyword in Java.

PageContext to obtain other implicit objects/forward jumps or include
13 session tracking-
Cookie/response. addCookie ()/request. getCookies ()/
URL rewriting implements parameter transfer/hiding form fields to implement parameter transfer/

HTTP is a stateless protocol. Each time a client opens a Web page, it establishes a new connection with the server and sends a new request to the server, the server processes client requests, returns a response to the client, and closes the connection with the client. When the client initiates a new request, it establishes a new connection with the server, so the server does not record any information about the customer.

Session-session refers to the time that has elapsed since you log on to the system and log out of the system. It is called a Session communication cycle. session sessions in jsp: A session starts when the client opens a browser and ends when the browser is closed or the session is exited. When the user re-opens one browser and re-enters the url, or accesses two web applications, different sessions are enabled; A session implicit object is created on the Web server and stored on the server. A session creates a session object. During a session, all programs in the web application share a session object.
Attribute related methods/isNew ()/getId ()/invalidate ()/setMaxInactiveInterval ()/
14 Servlet/ServletConfig/-> GenericServlet-> HttpServlet
Servlet workflow-(1) the client sends the request to the server.
(2) The Web container on the server instantiates the Servlet.
(3) The Web Container sends the request information to the Servlet.
(4) The Servlet creates a response and returns it to the Web container.
(5) The Web Container sends the response back to the client.
(6) Call the destory () method to exit when the server is closed or the Servlet's idle time exceeds a certain limit.
Lifecycle-create an instance-> init ()-> service ()-> destroy ()
Init ()-two methods: init ()/init (ServletConfig config) {super. init (config )}
Sevice (HttpServletRequest req, HttpServletResponse resp)

The Servlet architecture is built on the Java multithreading mechanism. When two or more threads access the same Servlet at the same time, multiple threads may access the same resource at the same time, data may become inconsistent. For member variables in Servet, thread synchronization measures must be taken to ensure thread security during concurrent access.

Destroy ()-when the server deletes a servlet instance call, the programmer can override it to complete cleaning, such as shutting down database connections.
15. Servlet API-javax.servlet and javax. servlet. http
Javax. servlet. GenericServlet-Protocol-independent
Javax. servlet. HttpServlet, -- embedded support for Http

Pass data to the client
In Servlet, you can directly write data to the output stream to generate an HTML page. Use PrintWriter to send the data back to the client.
PrinteWriter out = response. getWriter (); out. println ("<HTML> Inside HTML </HTML> ");
Before starting any output, you must use the setContentType () method to specify the MIME type.
Response. setContentType ("text/html, charset = gb2312 ");
16. To access servlet, you must configure it in web. xml.
<Servlet>
<Servlet-name> First </servlet-name>
<Servlet-class> com. servlet. FirstServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> First </servlet-name>
<Url-pattern>/first </url-pattern>
</Servlet-mapping>
17. WEB-INF \ classes directory to store Servlet class files
The WEB-INF \ lib directory stores the Java library files used by Web applications (JAR files)
18. The <init-param> defined by <servlet> in web. xml can be implemented through the servlet getInitParameter () method or the getServletConfig (). getInitParameter () method.
19. Filter chain
Request-> filter1-> filter2....-> Target Resource
Response <-.. filter <-

Javax. servlet. Filte Interface
Init ()
DoFilter (ServletRequest request, ServletResponse response, FilterChain chain) The Service Logic code of the filter is implemented in the doFilter () method.
{... Program segment 1...
Chain. doFilter (request, response );
... Program section 2...
}
Destroy ()
Javax. servlet. FilterConfig Interface
Javax. servlet. FilterChain Interface
 
Web. xml configuration
<Filter>
<Filter-name>
<Filter-class>
<Init-param>
</Filter>
<Filter-mapping>
<Filter-name>
<Url-pattern> // the url to be filtered
<Dispatcher> // filter the request type
</Filter-mapping>

The filter execution order is consistent with the order defined in web. xml.
20 The Listener is mainly used to listen on events in the Servlet container. After the event is listened on, the container activates the listener and performs the scheduled operation. The main event that the listener listens to is JSP.
Events that occur on the implicit object application, session, and request object mainly include the creation and destruction events of the hidden object, and the creation, modification, and sale of relevant scope variables.
Damage event.
Web. xml:
<Listener>
<Listener-class>
</Listener>

Javax. servlet. ServletRequestListener interface, mainly listening for the creation and destruction of implicit objects in requests
ServletRequestAttributeListener interface to listen for request attribute changes
Javax. servlet. http. HttpSessionListener interface to listen on the event of session Object creation and destruction in the context
Javax. servlet. http. HttpSessionAttributeListener interface to listen for changes in session attributes
Javax. servlet. ServletContextListener interface to listen on Initialization events and destruction events of the Web application Sevlet Context
Javax. servlet. ServletContextAttributeListener interface to listen on application attribute events
21. JavaBean
Javabean is a pojo class, plain old java objects, simple java object
Use JavaBean in jsp
1. new in <%>
2. jsp action <jsp: useBean> <jsp: setProperty> <jsp: getProperty>
Note the difference between class (new if not found) and type in userBean, and scope
Use external javabean, file upload, email, poi, etc.
22. Database Access-jdbc
Configure the database connection pool in tomcat \ conf \ context. xml <Context>
Javax. SQL. DataSource

Java. SQL. Blob blob = rs. getBlob ("");
Byte [] bytes = blob. getBytes (,);
23. MVC
Struts1.x
Controller-ActionServlet/Action
Struts-config.xml
ActionServlet reads the ActionMapping object in the Struts-config.xml, and each ActionMapping object implements the ing between a request and an Action object.
. The Action object calls javabean to complete business operations.
Process: login. jsp-> submit-> login. do-> web. xml-> servlet (ActionServlet, struts comes with)-> Read struts-config.xml-> action/actionform
(Form bean)-> execute ()
24. EL and JSTL
One of the purposes of introducing EL expressions to the EL expression language is to calculate JSP pages. It is convenient to access and print data, and Java code in JSP pages is minimized to make JSP pages
Simpler and easier to develop and maintain

The basic syntax of EL Expression Language is: $ {expression}
The EL expression uses the "." operator to access attributes in the JavaBean, $ {JavaBean name. attribute name}

The full name of JSTL is the JSP Standard Tag Library (JSTL). It is a technical specification proposed by SUN to simplify JSP page design;

*. The properties file is called a resource file. In the resource file, a variable and its value are stored in the form of "key = value", and a pair of "key = value" is written in one row "; it facilitates centralized storage and management of messages and internationalization of messages.

 

From I want to fly higher
 

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.