JSP quick Data Display

Source: Internet
Author: User
Tags classic asp

Younger brother in 2004 to learn the MS classic ASP the oldest ASP, non net. Now, JSP is officially used. Although the younger brother has been familiar with Java for a long time, he may know a little bit about it, but he does not have. Therefore, JSP should be used while learning. Take notes.
Learn about Tomcat

Tomcat is an HTTP server and a Web container. Tomcat consists of the following components:

  • Catalina (servlet container, the so-called servlet, should be a problem in today's Web framework such as route and distribution ),
  • Coyote (an underlying package for HTTP ctor processing HTTP headers and Response Headers)
  • Jasper (a JSP engine)
Built-in object

Built-in JSP page objects, which are both built-in and default. Each JSP page is provided. Like ASP, they are:

  ASP JSP
Returns some information about the server. Server

Actual type:

Org. Apache. Catalina. Core. standardwrapperfacadestandardwrapperfacade

Implementation interface javax. servlet. servletconfig

Page
Actual type: org. Apache. jsp. index_jspindex_jsp
Inheritance class org. Apache. Jasper. runtime. httpjspbasehttpjspbase
Inheritance class javax. servlet. http. httpservlet

Global Objects under the program Scope Application

Actual type:
Org. Apache. Catalina. Core. applicationcontextfacadeapplicationcontextfacade

Implementation interface javax. servlet. servletcontext

Client Requests Request Actual type:

Org. Apache. Catalina. connector. requestfacaderequestfacade

Implementation interface javax. servlet. http. httpservletrequest

Response Client Response Actual type:
Org. Apache. Catalina. connector. responsefacaderesponsefacade
Implementation interface javax. servlet. http. httpservletrequest
Session Seesion Actual type:
Org. Apache. Catalina. session. standardsessionfacadestandardsessionfacade
Implementation interface javax. servlet. http. httpsession
Page Context Objectcontext Pagecontext
Actual type:
Org. Apache. Jasper. runtime. pagecontextimplpagecontextimpl
Inheritance class javax. servlet. jsp. pagecontext
Exception None Exception

There should be many different documents in this regard, such as official ones. When the project goes deeper, learn how to use each API.

Insert HTML parts/Tag Reuse

The simplest method is <% @ include file = "Public/class. jsp" %>. asp is the same as JSP.

But unlike ASP, HTML cannot be mixed in methods. The following is not acceptable:

<% public static voidgetFooter(){%> <div>...</div><%}%>

Therefore, JSP labels must be used. The best thing is that you can use <% = var %> to read context variables.

<jsp:include page="bar.jsp" >    <jsp:param name="extraparam" value="TestTest" />    <jsp:param name="extraparam2" value="<%=a%>" /></jsp:include>

Include page, read parameters with $:

888-----------${param.extraparam}------${param.extraparam2}-------888

Although JSP pages are not as flexible as ASP pages,However, each include page has an independent context.In this case, the objects shared by the request, session, application, and other scopes can be used as intermediary media to transmit objects. They are essentially map, so you can set any type of objects through the setarrtivity () method. As follows:

<%   String name="zhouhaigang";  request.setAttribute("name", name);%>${ name }

More complex data types ......

// Import your user class at the beginning of the JSP page. List <user> userlist = (list <user>) request. getattribute ("list you sent"); int Len = userlist. size (); (...) {...}

Import JAR/classes
  • Use compiled class: place the *. Class file by namespace under the WEB-INF/classes directory. Then use import Iori. DB in JSP to call
  • Use jar package (jar package is a collection of many classes): Under normal circumstances, put in the WEB-INF/lib can be, this is also recommended practice; put it under/tomcat/common/lib, but the server will be automatically loaded when it is started, but too much will affect the startup speed. The last step is to introduce <% @ page import = "com. bbpoint. connect. * "%> <JSP: usebean id =" ASD "Scope =" page "class =" Connect. connect "/>. In this case, the jar package contains the corresponding classes. Jar is zip
    Compressed package, which can be viewed using tools such as 7zip. For an unknown jar package, it is best to use eclipse or decompilation to understand its API.

Why can I directly use it on the JSP page without using class/jar?

<%@page   import="java.sql.Connection"%><%@page   import="java.sql.DriverManager"%><%@page   import="java.sql.Statement"%><%@page   import="java.sql.ResultSet"%> 

Answer: Because tomcat has obtained your JDK/JRE package through the system environment variable classpath, you can directly import it.

How to compile a class? The advantage is that others will not easily see your code!

For details, see Tomcat + JSP classic configuration instance, which is well written.

How to define a method or class in JSP

To define a method, you must add an exclamation point after <%!

<%! // Here is the exclamation point private void amethod () {...;} // It can also be the private string avariableofclasss ;...... %>

This method is equivalent to redefining a method in servlet. If there is no exclamation point, it is a common situation, it indicates that in servlet. doservice () method write logic. You can see the mixed html/Java code in the generated Java class.

<% // There is no exclamation point here, and the code will be in the doservice () method...; %>

The strange thing is that classes can also be written in the doservice () method ????

Capture exceptions

Settings can capture exceptions explicitly thrown in the Code:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" errorPage="error.jsp"%>

Page for receiving exceptions

<%@page isErrorPage="true"%>

For unknown, non-captured exceptions, that is, 500 errors, we should use Tomcat's 500 err code to jump, but it is not conducive to debugging. How can we take both of them into account?

Debugging methods:

System. Out. println ("676" + uid_str );

Mylist. getclass (). getname ();

Out. Write (STR );

Set tomcat-compatible Chinese urls:

Add the uriencoding = "UTF-8" attribute to the connector node added in server. xml:

<Connector port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" URIEncoding="UTF-8" />

See

Why does the built-in JSP object not need to be declared?

Using Tomcat for simple JSP web development

JSP principles and tomcat configuration

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.