Summary of small issues in Java Web Development (Continuous updates)

Source: Internet
Author: User

1. getservletcontext (). getrealpath ("/") Here getrealpath ("/") needs to add "/" to indicate taking the absolute directory.

However, when running in WebLogic, tomcat, and jbosss, the results are inconsistent. The last character in the running result of Tomcat and JBoss is a directory separator, but not in weblogic. Example:

Tomat, JBoss:
String webpath = pagecontext. getservletcontext (). getrealpath ("/");
The webpath value is D: \ apache-Tomcat-6.0.24 \ webapps \ vanceinfoframework \

WebLogic:
String webpath = pagecontext. getservletcontext (). getrealpath ("/");
The webpath value is D: \ apache-Tomcat-6.0.24 \ webapps \ vanceinfoframework

 

To avoid bugs in the same application deployed on different servers, we recommend that you use pagecontext. getservletcontext (). getrealpath ("/") to determine whether the last character is a directory separator.

 

2. The index of the column name in the database starts from 1, as shown below:

 1 Connection conn = Null ;  2   3         Try  {  4   5             /**  Use the class. forname () method to automatically create this driverProgramAnd automatically call drivermanager to register it.  */  6   7 Class. forname ("oracle. JDBC. Driver. oracledriver" );  8   9            /**  Obtain the database connection through the getconnection () method of drivermanager  */  10   11 Conn = drivermanager. getconnection ("JDBC: oracle: thin :@ 172.16.6.222: 1521: orcl", "ewsvi_test", "EWS" );  12   13 Statement PS = Conn. createstatement ();  14   15 Resultset rs1_ps.exe cutequery ("select * From ewsvi. test_role" );  16  17             While  (Rs. Next ())  18   19   {  20   21 String name = Rs. getstring (1 ); //  The obtained value is the value of the first column. The index here does not start from 0.  22   23   System. Out. println (name );  24   25   } 26   27             28   29 } Catch  (Exception ex ){  30   31   Ex. printstacktrace ();  32   33 }

 

3. strut2 provides XML-basedValidate verification. We can use the following configuration in XML to verify the data transmitted from the foreground:

 1   <?  XML version = "1.0" encoding = "UTF-8" ?>  2   3   <!  Doctype validators public  4   "-// Opensymphony group // xwork validator 1.0.2 // en"  5   Http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"  >  6   7   <  Validators  > 8       <! --  User name is not empty, length, halfwidth, and allowed characters  -->  9       <  Field  Name  = "Examinee. username"  >  10           <  Field-validator  Type  = "Checkspecialchar"  Short-circuit = "True"  >  11               <  Param  Name  = "Cnvalue"  > True </  Param  >  12               <  Param  Name  = "Numvalue"  > True</  Param  >  13               <  Param  Name  = "Lettervalue"  > True </  Param  >  14               <  Param  Name  = "Specialchar" > @!. _- </  Param  >  15               <  Message  > The user name or email address can only contain Chinese characters, letters, numbers, and @!. _-Symbol! </  Message  >  16           </  Field-validator  >  17      </  Field  >  18       <! --  User name is not empty, length, halfwidth, and allowed characters  -->  19       <  Field  Name  = "Examinee. Password"  >  20           <  Field-validator  Type = "Checksqlinput"  Short-circuit  = "True"  >  21               <  Message  > Enter a special character! </  Message  >  22           </  Field-validator  >  23      </  Field  >  24   </  Validators  > 

Therefore, we must establish a connection between the action and the XML authentication file.

The relationship between the xml configuration file and action is as follows:

Someaction. Java-SomeAction-validation.xml

And it is in the same directory as someaction. Class. The SomeAction-validation.xml content is as above.

But sometimes we need to write the verification file for the specific method in the action, how should we write it?

For example, we have a login method in someaction, and we should create a SomeAction-login-validation.xml (extreme action name-method name-validation. XML)

The above method does not cause any problems, but pay attention to the situation. Let's take a look at the xml configuration first.Code:

1<ActionName= "Userlogin"Class= "Someaction"Method= "Login">

In this case, can we configure the XML verification file as above?Note the aboveName = "userlogin" and method = "login"

At this time, if you still use the SomeAction-login-validation.xml name, it will not be verified, must be changed to someaction-Userlogin-Validation. xml

 

4.Differences between mkdir and mkdirs

Mkdir (): Only one directory can be created.
For example, file = new file ("C:/AA ");
File. mkdir (); // you can create an AA directory under C :/.
If there are multiple levels of directories, mkdir cannot be used to create: file = new file ("C:/AA/BB"); file. mkdir () // cannot be created.
It should be changed:
Mkdirs ()

5. Differences between include and JSP: Include

Date. jsp

<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %> <% Java. util. date = new Java. util. date (); string date_cn = ""; string datestr = ""; Switch (date. getday () {Case 0: date_cn = ""; break; Case 1: date_cn = "1"; break; Case 2: date_cn = "2"; break; case 3: date_cn = "3"; break; Case 4: date_cn = "4"; break; Case 5: date_cn = "5"; break; Case 6: date_cn = "6"; break;} datestr = (1900 + date. getyear () + "year" + (date. getmonth () + 1) + "month" + date. getdate () + "Day (Week" + date_cn + ")";Document. Write ("<% = datestr %> ");%>

Test. jsp

<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312 "%> <HTML>  

<% @ Include file = "date. jsp" %> all the content of the other file will be converted to the JSP that references the page when the servlet is added during compilation.

This is because the include behavior element is executed in the request processing stage (here we need to describe the request processing stage. The JSP Container is responsible for converting the JSP page into a servlet in addition to the above mentioned,
It is also responsible for calling the JSP page implementation class to process each request and generate a response. This stage is called the request processing stage. The request processing stage is only an execution class file ).

Therefore, when we introduce the page as a <JSP: Include page = "relativeuri" Flush = "true"/> behavior element, we only reference date. servlet files generated after JSP files are converted and compiled. both,
Date. jsp is called only when the test. jsp file is run after execution as a separate file. Because the date. jsp file does not specify the character encoding, garbled characters appear.

At this time, the referenced JSP file will be compiled separately, and the referenced class method will be called on the referenced JSP page.

Execution time:

<% @ Include file = "relativeuri" %> is executed in the translation phase

<JSP: Include page = "relativeuri" flush = "true"/> it is executed in the request processing stage.

The introduced content is different:

<% @ Include file = "relativeuri" %> introduce static text (HTML, JSP) and combine it before the JSP page is converted into a servlet.

<JSP: Include page = "relativeuri" flush = "true"/> introduce the response text generated by the execution page or servlet.

 

6. Deployment location of the eclipse project after eclipse integrates Tomcat

E: \ workspace \. Metadata \. plugins \ org. Eclipse. WST. server. Core \ tmp0. This is the deployment location of the eclipse project after eclipse integrates tomcat.

 

7. The following exception occurs when Tomcat is started:

Severe: ioexception while loading persisted sessions: Java. Io. eofexception
Severe: exception loading sessions from persistent Storage

The reason is that Tomcat fails to read the hard disk session. A complete solution is to clear the files under \ work \ Catalina \ localhost, mainly *. or delete the session. ser can solve the problem.

8. If the prompt "cocould not connect to SMTP host" is always prompted when you send an email using javamail, and you are sure that the host, username, and passeword are correct, this problem is very problematic.

It may be caused by a firewall or 360. you can exit the firewall or 360 and resend it.

9. The (C: \ jboss-4.0.4.GA \ Server \ default) TMP and work directories under the JBoss Server Directory are cache directories.

Clear the cache and delete the contents in these two directories.

It may be caused by a firewall or 360. you can exit the firewall or 360 and resend it.

9. The (C: \ jboss-4.0.4.GA \ Server \ default) TMP and work directories under the JBoss Server Directory are cache directories.

Clear the cache and delete the contents in these two directories.

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.