The JSP1 of a rookie diary

Source: Internet
Author: User
Tags stack trace unique id

JSPThe full name is Java Server Pages, the Chinese name is the Java Servers page, it is a simplified servlet design, it is advocated by Sun Microsystems Company, many companies involved in the establishment of a dynamic Web technology standards. JSP technology is a bit like ASP technology, it is in the traditional Web page HTML (standard Common Markup Language subset) file (*.htm,*.html) inserted Java program segment (scriptlet) and JSP tag (tag), thereby forming a JSP file, suffix named (*.jsp).            Web applications developed with JSP are cross-platform and can run on Linux and other operating systems.            JSP is a technology that can develop dynamic Web pages. What is a Dynamic Web page: A Web page that can interact with the customer. Several components of JSP Reference

1. Template elements
HTML, XML
2. Annotation elements
(1) HTML comments, displayed in the client source code
<!--comments--
(2) JSP comments, will not be displayed in the client source code, only in the JSP
<%--Comment--%>
(3) Single-line comment and multiline comment
Single line//multiline/** Comment */

3. Instruction elements
(1) Page directive
<% @page %>

4. Script elements
(1) statement
<%! method or Variable%>
is converted to instance properties and instance methods in a servlet
(2) expression
<%=%>
Example: <%= "Hello, World"%>
Equivalent to the OUT.PRINTLN in JSP ("Hello, World");
Into the Out.print in the servlet ("Hello, World");

How to use JSP

JSP is a technology that can develop dynamic Web pages.

The steps to make a dynamic Web page in Java using JSP

1. Create a Web project

Fike "new" Web Project "named" to finalize

It was a success.2. The introduction of Tomcat (Apache Open source project) as a container for JSP. Window Preferences "Enter Server" to select the version (or a variety of options, just find the server tomcat) and then join the write. Web project to start the service down below is a below the 1. Initial page (can change the initial page in the XML file)
<body>    <form action= "2.jsp" method= "POST" >           <input type= "text" name= "name"  />     <br/>      <input type= "password" name= "pwd"  />      <br/>       <input type= "Submit" Name= " Chiole "value=" login "/>    </form>   </body>

Effect

The above code determines the sending address is 2.jsp, so if the steps are correct, it will enter the 2.jsp page code as follows
<%@ page language= "Java"Import= "java.util.*" pageencoding= "Iso-8859-1"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >String name= Request.getparameter ("name"); String pwd= Request.getparameter ("pwd"); if(Name.equals ("sa") &&pwd.equals ("sa")) {Session.setattribute ("Name", name); Response.sendredirect ("Welcome.jsp"); }Else{Request.getrequestdispatcher ("Index.jsp"). Forward (request, response); }                        %> <%=name%> <%=pwd%> </body>

The 2.jsp page receives the account number and password of the index.jsp page and determines that the account and password are set up to enter the Welcome page or the initial page of index.jsp.

Lose as correct

Input error

There's a Request,response object in the top of the list. They are 9 large built-in objects for JSPs (that is, objects that have been instantiated by Tomcat (or containers)) JSP9 large built-in objects (citing Baidu encyclopedia)Request Object   Response Object  Session ObjectOut ObjectPage ObjectApplication ObjectException ObjectPageContext ObjectConfig ObjectRequest ObjectThe client's request information is encapsulated in the requests object to understand the customer's needs and respond. It is an instance of the HttpServletRequest class. The request object has a requesting domain, which is valid until the client's request is completed. Ordinal method Description 1 Object getattribute (String name) returns the property value of the specified property 2 enumeration Getattributenames () returns an enumeration of all available property names 3 String Getcharacter Encoding () returns the character encoding 4 int getcontentlength () returns the length of the request body (in bytes) 5 String getcontenttype () Gets the MIME type of the request body 6 ServletInputStream getInputStream () gets the binary stream of a row in the request body 7 string GetParameter (string name) returns the parameter value of the specified parameter of name 8 enumeration Getparameternames () Returns an enumeration of the available parameter names 9 string[] Getparametervalues (string name) returns an array of all the values that contain the parameter name. String Getprotocol () returns the protocol type and version number of the request with a string Getscheme () returns the requested name of the program, such as: Http.https and FTP, etc. String getServerName () returns the server host name of the accepted request int Getserverport () Returns the port number used by the server to accept this request BufferedReader Getreader () returns the decoded request body, string getremoteaddr () returns the client IP address that sent this request. Getremotehost () returns the client hostname that sent this request. SetAttribute (String Key,object obj) sets the property value of the property to Stringgetrealpath (string path) Returns the true path of a virtual path. String Request.getcontextpath () returns the context pathResponse ObjectThe Response object contains information about the response to a customer request, but it is seldom used directly in the JSP. It is an instance of the HttpServletResponse class. The response object has a page scope, that is, when a page is accessed, the response object within that page is valid only for this access, and the response object on the other page is not valid for the current page. The ordinal method description 1 String getcharacterencoding () returns the response using what character encoding 2 Servletoutputstream Getoutputstream () returns a binary output stream of the response 3 PrintWriter Getwriter () Returns an object that can output characters to the client 4 void setcontentlength (int len) to set the response header length 5 void setContentType (String type) to set the response MIME type 6 sen Dredirect (java.lang.String location) REDIRECT client requestSession ObjectThe session object refers to the client-to-server conversation, starting from the client connection to a webapplication of the server until the client disconnects from the server. It is an instance of the HttpSession class. The session object is a JSP built-in object that is created automatically when the first JSP page is loaded, and completes conversation-period management. Open a browser from a client and connect to the server to start, to the client close the browser to leave the end of this server, known as a session. When a client accesses a server, it may switch between several pages of the server, and the server should know by some means that it is a client and needs a session object. The session object has a conversation scope. Ordinal method Description 1 long GetCreationTime () returns session creation time 2 public String getId () returns session created when the JSP engine set its unique ID number 3 long getlastaccessedti Me () returns the last request time of the client in this session 4 int getmaxinactiveinterval () returns two request interval how long this session was canceled (MS) 5 string[] GetValueNames () Returns an array containing all the available properties in this session 6 void invalidate () cancels the session, making session unavailable 7 Boolean isnew () returns a session created by the server, whether the client has joined 8 void RemoveValue (String name) removes the attribute specified in session 9 void Setmaxinactiveinterval () set two request interval how long this session was canceled (MS) Soft channel quotations JSP built-in object JSP built-in object is a Java class object that can be used in JSP pages.Out ObjectAn Out object is an instance of the JspWriter class, a common object ordinal method that outputs content to the client. 1 void Clear () clears the contents of the buffer 2 void Clearbuffer () clears the current contents of the buffer 3 void flush () empties the stream 4 int GetBufferSize () returns the buffer in the size of the number of bytes, if the buffer is not set to Getremaining () The return buffer is still remaining as much as 6 Boolean Isautoflush () returns the buffer full, whether it is automatically emptied or throws an exception 7 void Close () to close the output streamPage ObjectThe Page object is a pointer to the current JSP page itself, a bit like the this pointer in the class, which is an instance of the Java.lang.Object class method description 1 class GetClass returns this object of 2 int hashcode () Returns the hash code of this object 3 Boolean equals (Object obj) to determine whether this object is equal to the specified Object object 4 void Copy (object obj) to copy this object to the specified Object object 5 The object Clone () clone of this Object object 6 string toString () converts this object to a string Class 7 void Notify () wakes up a waiting thread 8 void Notifyall () wakes up all waiting lines Process 9 void Wait (int timeout) causes a thread to wait until timeout ends or wakes up with a thread waiting until it wakes up one after the other () Entermonitor () to object locking-void Exi TMonitor () lock on objectApplication ObjectThe Application object implements the sharing of data among users and can store global variables. It starts at the start of the server until the server shuts down, during which time this object will persist, so that the same properties of the object can be manipulated on the user's back-and-forth connection or in a connection between different users, and the operation of this object property anywhere will affect access to it 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 ordinal method description 1 Object getattribute (String name) returns the property value of the given Name 2 enumeration Getattributenames () returns the enumeration of all available property names 3 void SetAttribute (St Ring Name,object obj) Set property value of attribute 4 void RemoveAttribute (string name) Delete one property and its property value 5 String getserverinfo () return JSP (SERVLET) Engine name and version number 6 string Getrealpath (string path) returns the true path of a virtual path 7 ServletContext getcontext (String uripath) Returns the Application object of the specified WebApplication 8 int getmajorversion () returns the maximum version number of the Servlet API supported by the server 9 int getminorversion () Returns the minimum version number of the Servlet API supported by the server. String GetMimeType (String file) returns the MIME type of the specified file, one URL getresource (string path) returns the specified resource (file and directory) The URL path of InputStream getresourceasstream (string path) returns the input stream of the specified resource RequestDispatcher getrequestdispatcher (string Uripath) returns the RequestDispatcher object of the specified resource. Servlet getservlet (String name) returns the SERVLET15 enumeration Getservlets () for the specified name Returns an enumeration of all Servlets enumeration GetSErvletnames () Returns an enumeration of all servlet names (String msg) writes the specified message to the servlet's log file, with a void log (Exception exception,string msg) Writes the stack trace and error message of the specified exception to the servlet log file, msg,throwable log (String throwable) writes the stack trace and the description of the given Throwable exception to the servlet's log fileException ObjectThe exception object is an exception object that is created when a page has an exception during the run. If a JSP page is to be applied to this object, the Iserrorpage must be set to true, otherwise it cannot be compiled. He's actually a java.lang.Throwable. Object ordinal method Description 1 String getMessage () returns a message describing the exception 2 string toString () returns a short description of the exception message 3 void Printstacktrac E () show exception and its stack trace 4 throwable fillinstacktrace () Override exception's execution stack trajectoryPageContext ObjectThe 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, or a property value of the application that is located on the pages, which is the equivalent of all the features in the page. Its class name is also called PageContext. Ordinal method Description 1 JspWriter getout () returns the current client response is used by the JspWriter stream (out) 2 HttpSession getsession () returns the HttpSession object in the current page (session) 3 Obje CT GetPage () Returns the object object of the current page (page) 4 servletrequest getrequest () returns the ServletRequest object of the current page (request) 5 Servletresponse GetResponse () returns the Servletresponse object of the current page (response) 6 Exception getexception () returns the Exception object of the current page (Exception) 7 ServletConfig Getservletconfig () returns the ServletConfig object (config) of the current page 8 ServletContext getservletcontext () Returns the ServletContext object of the current page (application) 9 void SetAttribute (String name,object attribute) setting properties and property values of ten void SetAttribute ( String Name,object obj,int Scope sets properties and property values in the specified range all public object GetAttribute (string name) takes the value of the property of the object GetAttribute (St Ring Name,int Scope) takes the value of the property within the specified range, public Object findattribute (String name) looks for a property, returns the property value, or NULL14 void RemoveAttribute ( String name) Removes a property of RemoveAttribute (string name,int scope) to delete a property in the specified range of Getatt intRibutescope (String name) returns the scope of a property enumeration getattributenamesinscope (int scope) Returns the property names available in the specified range enumeration of void release () Frees the resource occupied by PageContext forward (string relativeurlpath) to redirect the current page to another page, void include (String relativeurlpath) Include another file in the current locationConfig ObjectThe Config object is used by the JSP engine to pass information to it when a servlet is initialized, including the parameters to be used when the servlet initializes (through property names and property values) and information about the server (by passing a ServletContext object) ordinal Method Description 1 ServletContext getservletcontext () returns the ServletContext object containing information about the server 2 string getinitparameter (string name) Returns the value of the initialization parameter 3 enumeration Getinitparameternames () returns an enumeration of all parameters required by the servlet initializationtiming of built-in object generation a JSP page corresponds to a servlet class with three methods per servlet class:
Init method: Initializes the Jsp/servlet method.
Destory method: Destroys the Jsp/servlet method.
Service method: The method that responds to a user request.
The request object and the response object are the parameters of the service method, the Application object, the Page object, the Out object, the PageContext object, and the session object are the instances generated in the service's method. These are described in more detail in struts ' self-media sites.

The JSP1 of a rookie diary

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.