Some details of jsp,servlet,mysql technology to build dynamic website

Source: Internet
Author: User

Today's shopping sites, more use of JSP and servlet technology, connected to the database for the dynamic display of the page and database read and write. Then I wrote some of the questions are encountered in the design of similar technology, dedicated to some beginners like me, for you to settle some difficulties. Ask the gods to ignore them.

1.java connection MySQL database of the specific process here not to explain in detail, self-Baidu "JDBC Connection MySQL database", The problem here is to be sure to import the package mysql-connector-java-x.x.x, which is generally no problem, but it is also important to note that you must add "package path" + "\ Package name" in the system variable classpath of the environment variable.
2. We all know that you need to declare the action= "servlet" in the form if you invoke the servlet with a JSP, but be careful to add it in your Web. XML before </web-app>
<servlet>
<servlet-name>servlet's name </servlet-name>
<servlet-class>servlet the package. servlet name </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet's name </servlet-name>
<url-pattern>/servlet's name </url-pattern>
</servlet-mapping>
Each servlet adds a piece of code above it.

3. Download the problem, if you want to click on the link to download the code to write, if it is done in the JSP page, please note that the two types of code in the first line is not the same place

<% @page contenttype= "text/html;charset=gb2312" import= "com.jspsmart.upload.*"%><%
Create a new Smartupload object
Smartupload su=new smartupload ();
Initialization
Su.initialize (PageContext);
/*
* Set contentdisposition to NULL to prevent the browser from automatically opening files, ensure that after clicking the link is the download file.
* If not set, the browser will automatically open it with Word when the file name extension doc is downloaded.
* When the extension is PDF, the browser will open with Acrobat
*/
Su.setcontentdisposition (NULL);
Download file
Su.downloadfile ("/upload/. jpg");
%>

Instead of

<% @page contenttype= "text/html;charset=gb2312" import= "com.jspsmart.upload.*"%>

<%
Create a new Smartupload object
Smartupload su=new smartupload ();
Initialization
Su.initialize (PageContext);
/*
* Set contentdisposition to NULL to prevent the browser from automatically opening files, ensure that after clicking the link is the download file.
* If not set, the browser will automatically open it with Word when the file name extension doc is downloaded.
* When the extension is PDF, the browser will open with Acrobat
*/
Su.setcontentdisposition (NULL);
Download file
Su.downloadfile ("/upload/. jpg");
%>

No other characters can be inserted between "%>" and "<%"

4. If you want to upload in a servlet instead of doing it in a JSP page, be careful to add it before the code "su.initialize (PageContext);//Initialize upload operation"

Jspfactory jspxfactory = null;
PageContext pagecontext = null;
Jspxfactory = Jspfactory.getdefaultfactory ();
PageContext = Jspxfactory.getpagecontext (This,request,response, "", true,8192,true);
The reason is that Smartupload is a JSP built-in object, so implementation in the servlet requires the following processing

5. If you want to implement both forms and uploads in the servlet, because the table only son value is generally string str=request.getparameter ("parameter"), but because the Smartupload package also has the request object, So the way in which the values should be passed into the form

String str=su.getrequest (). GetParameter ("parameter");

6. Note the setting of the character set, next I say the character set is Utf-8 settings

(1) Character set settings for JSP pages

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >

(2) Setting the servlet character set

Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");

(3) Set of character sets to pass values from the JSP page to the servlet

New String (Request.getparameter ("id"). getBytes ("Iso-8859-1"), "UTF-8");

(4) If the MySQL character set is utf-8, note here that when JDBC connects to the database, the required characterencoding=gb2312

There's no mistake here, it's gb2312, don't ask me why I don't know

In general on these details, I hope to help you, the great God do not spray.

Some details of jsp,servlet,mysql technology to build dynamic website

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.