Sixteen common JSP Problems and Solutions

Source: Internet
Author: User

The programming power comes from solving the problem. What are the common JSP problems? The following are 16 common JSP problems to be presented in this article.

In the process of JSP programming will encounter a variety of problems, then we will summarize 16 aspects to share with programmers.

Jsp faq 1:

Form request Chinese Character Processing:

 
 
  1. request.setCharacterEncoding("GB2312") 

Jsp faq 2:

Obtain the Page name from the JSP page:

 
 
  1. Request. getRequestURI (); // file name
  2. Request. getRequestURL (); // All URLs

Jsp faq 3:

The page does not retain the cache:

 
 
  1. response.setHeader("Pragma","No-cache");  
  2. response.setHeader("Cache-Control","no-cache");  
  3. response.setDateHeader("Expires", 0);  
  4.  

Jsp faq 4:

Date and Time Server)

 
 
  1. String datestr;
  2. Java. text. DateFormatDf=NewJava. text. SimpleDateFormat ("MM dd HH: mm E"); // format it here
  3. Datestr=Df. Format (new java. util. Date ());
  4. Out. println (datestr );
  5.  

Or

 
 
  1. ﹤% java.util.Date shijian= new java.util.Date();%>  
  2. ﹤%=shijian.getYear()+1900%>﹤%=shijian.getMonth()+1%>﹤%=shijian.getDate()%>  
  3. ﹤%=shijian.getHour()%>﹤%=shijian.getMinute()%>  
  4.  

Jsp faq 5:

Use regular expressions in java.

Jdk 1.4 or above import = "java. util. regex .*

Jsp faq 6:

Click Back to display page expiration

Code in it

 
 
  1. ﹤META http-equiv=Pragma content=no-cache﹥  
  2. ﹤META http-equiv=Cache-Control content=no-cache﹥  
  3. ﹤META http-equiv=Expires content=0﹥  
  4.  

Same as 3.

Jsp faq 7:

Swtich cannot work on long and String:

In switchexpr1), expr1 is an integer expression. The parameters passed to the switch and case statements should be
Int, short, char, or byte. Long and string cannot apply to swtich.

Jsp faq 8:

Computing execution time

Code start time, end time, subtract

 
 
  1. long t1 = System.currentTimeMillis();  
  2. .... ...... ....  ...your code  
  3. long t2 = System.currentTimeMillis() ;  
  4. long time = t2-t1;  
  5.  

Jsp faq 9:

Is it rounded to the nearest decimal point?

 
 
  1. import java.text.*;  
  2. NumberFormat nf=NumberFormat.getNumberInstance();  
  3. nf.setMaximumFractionDigits(2);  
  4. nf.setMinimumFractionDigits(2);  
  5. nf.format(numb);  

Or (+ 0.005-0.01) Fetch the last two digits.

Jsp faq 10:

The default form method is get.

The post method is used to transmit large data volumes to the server. Open another socket .?)

Jsp faq 11:

Prevent users from entering the url directly on the page:

◆ Add control to the page to be accessed. This is generally used session.

◆ It is controlled from the web server. All access to a directory must be verified. Some people say that jsp should be placed under web-inf)

Jsp faq 12:

The database is of the datetime type, and the current time is inserted into the database:

 
 
  1. java.sql.Date sqlDate = new java.sql.Date();  
  2. PreparedStatement pstmt = conn.prepareStatement("insert into foo(time) values(?)");  
  3. pstmt.setDate(1,sqlDate);  
  4. pstmt.executeUpdate();  
  5.  

Generally, databases have their own system time functions.

Insert into foo (time) values (sysdate)

Jsp faq 13:

Session:

 
 
  1. Session. setAttribute ("int", I + ""); // note that I + ""
  2. IntI=Integer. ParseInt (session. getAttribute ("int "));
  3.  

Session concepts.

When a user browses a webpage, because Http is a stateless protocol, there is often a data exchange problem between different pages, this requires data sharing between these different pages. A common implementation method is to save the data to be shared to the Session. For example, on the user login page, save some user information to the Session, and then read the user information on other pages. The shared data can be a string or an object related to the original data type of Java, or a Java object.

The Session can only save objects, but cannot save the original data type, for example:

Session. setAttribute ("count", 10)

The statement is invalid. To save an integer of 10 to the Session, use the following method:

Session. setAttribute ("count", new Integer (10 ));

Then use the following in another page:

(Integer) session. getAttribute ("count ")

To read this integer.

In was, sessions can be stored in the system database, but this affects the efficiency. The session size should not be too large.

Jsp faq 14:

Convert characters into ASCII codes

Int a = 'a'; out. println ();

Jsp faq 15:

String s = new String ("xyz"); Two String Object objects are created. One is "xyx" and the other is the reference Object s pointing to "xyx.

There is also a typical equals () and = reference in the stack principle.

Jsp faq 16:

Hashtable and HashMap

Hashtable inherits from the Dictionary class, while HashMap is an implementation of the Map interface introduced by Java1.2. HashMap allows null as the key or value of an entry, while Hashtable does not allow

The Hashtable method is Synchronize, but HashMap is not. When multiple threads access Hashtable, they do not need to implement synchronization for their own methods, and HashMap must provide external synchronization for it.

  1. JSP tutorials-basic articles-simple homepage Creation
  2. JSP tutorial basics: Technical Features of JSP
  3. Specific algorithms for implementing the tree structure of JSP forums
  4. JSP2.0 features of basic JSP tutorial knowledge
  5. JSP tutorial-traffic count JSP source code

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.