Chapter Two. Jsp/servlet and related technical explanation

Source: Internet
Author: User

4 basic syntax for JSPs:

1.JSP notes:

<%--JSP comment Section--%>

2.JSP declaration:

<%! Declaring an integer variable

public int count;

Declaring a method

Public String info () {

return "Hello";

}

%>

3. Output the JSP expression:

<%=count++%>

This phrase replaces the Java out.print (count++); note, however, that there cannot be a semicolon after the output expression.

4.JSP script:

1<%@ page contenttype= "text/html; CHARSET=GBK "language=" java "errorpage=" "%>234<title> Welcome </title>56<body>7<table bgcolor= "#9999dd" border= "1" width= "300px" >8<%9  for(inti = 0; I < 10; i++){Ten%> One<tr> A<td> Cyclic value:</td> -<td><%=i%></td> -</tr> the<% - } -%> -</body> +View Code

3 Compiler directives for JSPs: (only two here)

1.page directive: At the top of the JSP page, a JSP page can be a multiple page instruction.

Page syntax format: <% @page contenttype= "text/html; CHARSET=GBK "language=" java "...%>

ContentType Property Reference page: http://developer.51cto.com/art/201106/270666.htm

2.include directive: You can embed an external file into the current JSP file and parse the JSP statements in the page.

Include can contain both static text and dynamic JSP pages. The static include compilation directive will include the included page into this page, fused into a page, so the included page does not need to be a complete

Page.

Static include format: <% @include file= "Relativeurlspec"%>

7 Action commands for JSP:

1.forward directive: Used to forward a page response to another page. It can be forwarded to a static HTML page, forwarded to a dynamic JSP page, or forwarded to a servlet in a container.

2.include dynamic instruction: Used to include a page, the included page does not import the containing page, only the body content of the included page is inserted into this page.

3.useBean directive: Used to initialize a Java instance on a JSP page

4.setProperty directives: To set values for the properties of an JavaBean instance

5.getProperty directive: A property used to output an JavaBean instance

If multiple JSP pages need to reuse a piece of code, you can define the code as a Java class method, and then have multiple JSP pages invoke the method, which can be reused.

6.plugin instruction: Primarily used to download server-side JavaBean or applets to client execution. Because the program executes on the client, the client must install the virtual machine

7.param directive: Used to set the parameter value, the instruction cannot be used alone, can be used in conjunction with Jsp:include, Jsp:forward, jsp:plugin three instructions.

9 built-in objects in a JSP script:

These 9 built-in objects are instances of the Servlet API interface, but the JSP specification initializes them by default (there is a JSP page corresponding to the servlet's _jspservice () method to create these instances). That they are already objects that can be directly

Use.

An instance of 1.application:javax.servlet.servletcontext that represents the Web application itself that the JSP belongs to, which can be used for JSP pages or for exchanging information between servlets.

An instance of 2.config:javax.servlet.servletconfig that represents the configuration information for the JSP.

An instance of 3.exception:java.lang.throwable used to represent exceptions and errors in other pages. This object can be used only if the page is an error-handling page, that is, the Iserrorpage property of the compile instruction page is true.

An instance of 4.out:javax.servlet.jsp.jspwriter that represents the output stream of a JSP page, which is used to output content and form an HTML page.

5.page: Represents the page itself, that is, the this in the servlet, whose type is the generated servlet class, which can be used where the page is.

An instance of 6.pagecontext:javax.servlet.jsp.pagecontext that represents the JSP page context used by the object to access shared data in the page.

An instance of 7.request:javax.servlet.http.httpservletrequest that encapsulates a request and the client's request parameters are encapsulated in the object, and the object must be used to obtain the client request parameters.

An instance of 8.response:javax.servlet.http.httpserletresponse that represents the response of the server to the client. Typically, the object is rarely used directly in response, but an out object is used, unless a non-character response needs to be generated.

Response objects are often used for redirection.

An instance of 9.session:javax.servlet.http.httpsession that represents a session. The session begins when the client browser and the site establish a connection, and the session ends when the client closes the browser.

Servlet Introduction:

The nature of the JSP is the servlet, and after the developer deploys a well-written JSP page in the Web container, the Web container compiles the JSP into the corresponding servlet.

The disadvantage of using Servlets directly: Servlets are very inefficient to develop, especially when using servlets to generate presentation-layer pages, all HTML tags in the page are output by the servlet output stream, which is extremely cumbersome.

Since the MVC specification has emerged, the responsibility of the servlet begins to be clarified, used only as a controller, not required to generate page labels, and is no longer used as a view-layer role.

Development of Servlets:

A servlet, called a server-side applet, is a program that runs on the server and is used to process and respond to requests from clients.

The servlet is a special Java class that inherits from HttpServlet.

The servlet provides different methods for responding to client requests:

1.doGet: Respond to a client's GET request

2.doPost: Responding to client post requests

3.doPut: Responding to a client put request

4.doDelete: Responding to client delete requests

Client requests are typically only get and post two, and Servlets must override the Doget () and Dopost () two methods in response to both requests.

Most of the time, the servlet responds exactly the same to all requests. In this case, the override service (HttpServletRequest request, HttpServletResponse Response) method can be used instead of the above methods.

Can respond to all requests from the client.

Instead of writing a constructor for a servlet class, to perform an initialization operation on the servlet, the initialization should be placed in the servlet's init () method. If you override the init (ServletConfig config) method, you should override the method's

The first line calls Super.init (config). This method will call the Init method of HttpServlet.

1  PackageLee;2 3 Importjavax.servlet.*;4 Importjavax.servlet.http.*;5 Importjavax.servlet.annotation.*;6 7 ImportJava.io.*;8 9 //Servlets must inherit the HttpServlet classTen@WebServlet (name= "Firstservlet" One, urlpatterns={"/firstservlet"}) A  Public classFirstservletextendsHttpServlet - { -     //The response method of the client that can be used to respond to all types of requests from the client the      Public voidService (HttpServletRequest request, - httpservletresponse Response) -         throwsservletexception,java.io.ioexception -     { +         //Setting the decoding mode -Request.setcharacterencoding ("GBK"); +Response.setcontenttype ("TEXT/HTML;CHARSET=GBK"); A         //gets the request parameter value for name atString name = Request.getparameter ("name"); -         //get the request parameter value for gender -String gender = Request.getparameter ("Gender"); -         //get the request parameter value for color -string[] color = request.getparametervalues ("Color"); -         //get the request parameter value for country inString national = request.getparameter ("Country"); -         //get page output stream toPrintStream out =NewPrintStream (Response.getoutputstream ()); +         //output HTML page labels -Out.println ("); theOut.println ("); *Out.println ("<title>servlet test </title>"); $Out.println (");Panax NotoginsengOut.println ("<body>"); -         //value of output request parameter: Name theOut.println ("Your name:" + name + "); +         //value of output request parameter: Gender AOUT.PRINTLN ("Your Gender:" + Gender + "); the         //value of Output request parameter: Color +Out.println ("You Like the color:"); -          for(String c:color) $         { $Out.println (c + "")); -         } -Out.println ("); theOut.println ("You Like the color:"); -         //value of Output request parameter: NationalWuyiOUT.PRINTLN ("Country You are from:" + National + "); theOut.println ("</body>"); -Out.println ("); Wu     } -}
View Code

The differences between Servlets and JSPs are:

There are no built-in objects in 1.Servlet, the built-in objects in the original JSP must be created by the program display

2. For static HTML tags, the servlet must use a page output stream to output rows

JSP is a simplification of the servlet, the use of JSP only need to complete the programmer to output to the client's content, JSP script embedded in a class by the JSP container to complete.

The servlet is a complete Java class, and the Service () method of this class is used to claim a response to the client.

Configuration of the servlet:

The edited servlet source file does not respond to user requests, and it needs to be compiled into a class file. Place the compiled class file under the Web-inf/classes path, and if the servlet has a package, create it under the web-inf/classes path

The corresponding folder.

Starting with Servlet3.0, there are two ways to configure a servlet:

1. Configure using @webservlet annotations in the servlet class: reference URL: http://blog.csdn.net/kpchen_0508/article/details/41312735

2. Configure in the Web. xml file: Reference URL: http://01121264-163-com.iteye.com/blog/1530063

Jsp/servlet's declaration period:

  

Chapter Two. Jsp/servlet and related technical explanation

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.