JSP and tomcat knowledge sorting

Source: Internet
Author: User

I. Differences between two types of include in JSP
1. <% @ include file = "..." %>
2. <JSP: Include page = "..."/>
Or
<JSP: Include page = "...">
<JSP: Param name = "..." value = "..."/>
</Jsp: Include>

Method 1 includes post-processing, and method 2 is processing and then including.
When method 1 is used, if the included file contains the same variable definition as the main file, an error occurs. method 2 is to process the code in the included file and only include the result. Recommended method 2.

Ii. configuration of the Tomcat server virtual directory
Open the tomcat installation directory, enter CONF/server. XML, and add
<Context Path = "/virtual directory name" docbase = "virtual directory path"/>

Iii. Difference Between equls and = in Java
Equls is a comparison of the content, = is a comparison of the address.
Tip: use the "string" when using equls ". equls (variable name) to avoid using the variable name. equls ("string") exception occurs when the variable name is null.

Iv. Nine built-in JSP objects (built-in object type scope)
· Servlet-related hidden objects
Config javax. servlet. servletconfig page
Page Java. Lang. Object page

· Hidden objects related to input/output
Request javax. servlet. http. httpservletrequest request
Response javax. servlet. http. httpservletresponse page
Out javax. servlet. jsp. jspwtiter page

· Provide implicit context objects during JSP execution
Pagecontext javax. servlet. jsp. pagecontext page
Session javax. servlet. http. httpsession session
Application javax. servlet. servletcontext Application

· Hidden objects related to error
Exception java. Lang. throwable page

V. Differences between the two jumps
1. <JSP: Forward page = "..."/>
· The Address Bar does not change (server jump)
· Jump immediately after the jump statement is executed, and subsequent code will not be executed
· If you use forward redirection, you must release all resources before the redirection.
· When forward is used, the attributes set for the request can still be stored on the next page (setattribute)
· Pass parameters through <JSP: Param name = "..." value = "..."/>

2. response. sendredirect ("...");
· Address bar change (client jump)
· Skip after all the code is executed
· The request attribute cannot be saved (the address is changed and the client jumps)
· Passing parameters through URL address Rewriting

In addition, use this statement in servlet to implement JSP: Forward jump
Resquest. getrequestdispatcher ("XXX. jsp"). forwaor (req, resp );

6. Other web. xml configurations
<Servlet>
<Servlet-Name> mldn </servlet-Name>
<JSP-File>/WEB-INF/sdemo. jsp </JSP-File> (file path)
<Init-param>
<Param-Name> XXX </param-Name>
<Param-value> AAA </param-value>
</Init-param>
<Init-param>
<Param-Name> YYY </param-Name>
<Param-value> BBB </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-Name> mldn </servlet-Name>
<URL-pattern>/lxh </url-pattern> (the address corresponding to the browser is mapped to the file path above)
</Servlet-mapping>
<Welcome-file-List> (default homepage settings)
<Welcome-File> default.html </welcome-File>
<Welcome-File> default.htm </welcome-File>
<Welcome-File> default. jsp </welcome-File>
</Welcome-file-List>

The value in init-Param is obtained through config. getinitparameter ("XXX.

VII. Database Operation Sequence
<% @ Page import = "Java. SQL. *" %>
1. Load the driver
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
2. Connect to the database
Connection conn = drivermanager. getconnection ("JDBC: ODBC: XXX ");
3. operate databases
Statement stmt = conn. createstatement ();
Stmt.exe cuteupdate ("..."); (insert, delete, change)
Resultset rs = stmt.exe cutequery ("..."); (query)
While (Rs. Next ())
{
Int id = Rs. getint ("ID ");
Steing name = Rs. getstring ("name ");
...
Int id = Rs. getint (1 );
Steing name = Rs. getstring (2 );
}
4. Shut down the database
Rs. Close ()
Stmt. Close ();
Conn. Close ();

8. preparedstatement
SQL = "insert into person (name, password, age) values (?,?,?) ";
Preparedstatement pstmt = conn. preparestatement (SQL );
Pstmt. setstring (1, XXX );
Pstmt. setstring (2, YYY );
Pstmt. setint (3, zzz );
Pstmt.exe cuteupdate ();

9. cursor and resultset parameters (JDBC 2.0)
Read-Only cursor
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_read_only );
Rs. afterlast (); // move the cursor to the end, which is equal to Rs. Absolute (-1 );
Rs. Absolute (3); // output from Article 4
Rs. Previous (); // scroll forward

Updatable
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Rs = stmt.exe cutequery (SQL );
Rs. Last ();
Rs. updatestring ("name ","...");
Rs. updatestring ("password ","...");
Rs. updaterow ();

Insert
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Rs = stmt.exe cutequery (SQL );
Rs. movetoinsertrow ();
Rs. updatestring ("name ","...");
Rs. updatestring ("password ","...");
Rs. insertrow ();

Delete
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Rs = stmt.exe cutequery (SQL );
Rs. Absolute (4 );
Rs. deleterow ();

All of the above operations are performed to locate all the data in the memory. The performance is poor and is not recommended.

10. Batch Processing
Try
{
Stmt = conn. createstatement ();
Conn. setautocommit (false); // Cancel automatic submission
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ a', 'zzzzzz ', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ B ', 'zzzzzz', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ C', 'zzzzzz ', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ d', 'zzzzzz ', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ E', 'zzzzzz ', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ F', 'zzzzzz ', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ G', 'zzzzzz ', 28 )");
Stmt. addbatch ("insert into person (name, password, age) values ('lxh _ H', 'zzzzzz ', 28 )");
Stmt.exe cutebatch (); // executes the batch processing statement.
// If no exception exists, execute this code segment
Conn. Commit (); // submit a transaction to insert data into the database.
}
Catch (exception E)
{
Try
{
Conn. rollback (); // roll back the database
}
Catch (exception E1)
{}
Out. println ("database operation failure !!! ");
}

11. How to apply the modified JavaBean
1. Restart the Tomcat server
2. Modify the conf/server. xml file of Tomcat
<Context Path = "/virtual directory name" docbase = "virtual directory path" reloadable = "true"/>
Add the red attribute. But it will reduce the server performance, which is generally used only in development.

12. Use Javabean
<JSP: usebean id = "XXX" Scope = "yyy" class = "ZZ. ZZ. ZZ"/>

<JSP: setproperty name = "XXX" property = "*"/>
· Name is a declared JavaBean object.
· If property is *, it indicates automatic matching. You can also specify a single variable.
<JSP: setproperty name = "XXX" property = "A" Param = "B"/>
· Pass the obtained value of B to
<JSP: setproperty name = "XXX" property = "A" value = "..."/>
· Pass custom values to

<JSP: getproperty name = "XXX" property = "A"/>
· Get the value of variable

Scope:
Page, only valid on the current page, applicable to database operations
Request. The property is only saved in one server jump. You can use <JSP: Forward>
Session. attributes are stored in one session. This attribute is applicable to the Development of shopping carts.
Application, attribute public, this object is instantiated only once on the server

13. Use the smartupload component

HTML:
<Form action = "XXX. jsp" method = "Post" enctype = "multipart/form-Data">

JSP:
<JSP: usebean id = "smart" Scope = "page" class = "xx. xx. xx">
<%
// 1. Initialization
Smart. initalize (pagecontext );
// 2. Prepare for upload
Smart. Upload ();
// 3. Save the uploaded file
Smart. Save ("/upload ");
%>

* For uploading images, because they are generally large, they can only be submitted using post.

If the form is encapsulated (enctype is used), you cannot use request to directly obtain the input parameter. In this case, you must use smartupload:

Copy smartupload. jar to Tomcat/common/lib.

Get the extension name of the uploaded file: smart. getfiles (). GetFile (0). getfileext ();
Save the file: smart. getfiles (). GetFile (0). saveas ("/upload ");

14. Enable Tomcat's directory list Function
Open CONF/Web. xml and find

<Servlet>
<Servlet-Name> default </servlet-Name>
<Servlet-class> org. Apache. Catalina. servlets. DefaultServlet </servlet-class>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 0 </param-value>
</Init-param>
<Init-param>
<Param-Name> listings </param-Name>
<Param-value> false </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

Set false under listings to true.

15. Set the Tomcat management password
Edit the tomcat-users.xml for the conf directory
<? XML version = '1. 0' encoding = 'utf-8'?>
<Tomcat-users>
<Role rolename = "Tomcat"/>
<Role rolename = "role1"/>
<Role rolename = "admin"/> // Add
<Role rolename = "manager"/> // Add
<User Username = "Tomcat" Password = "Tomcat" roles = "Tomcat"/>
<User Username = "both" Password = "Tomcat" roles = "tomcat, role1"/>
<User Username = "role1" Password = "Tomcat" roles = "role1"/>
<User Username = "..." Password = "..." roles = "Admin, Manager"/> // Add
</Tomcat-users>

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.