Struts garbled Solution

Source: Internet
Author: User

Struts garbled Solution
There was no place for the National Day, so I learned about struts and made a news management system using struts;
However, Chinese characters inserted into the database are always garbled. After a morning's efforts, I finally solved the Garbled text problem. The following is the process of searching and solving Garbled text:
1. Since the content displayed on the webpage is garbled, you must first determine whether the content in the database can be correctly displayed. The database I use is mysql4.1,
Open the console and select * from news; sure enough, the content inserted in the database is garbled. Then I try to manually insert Chinese characters,
An error occurs. How can this problem be solved? The Data Type I used is varchar (100), and the inserted data is only three Chinese characters. It should not be too long,
Later I thought it might be the encoding problem. So I deleted the original table, rebuilt it, and set the encoding to gb2312.
Create Table News (
Id int primary key,
Title varchar (100) not null,
Content varchar (400) not null,
Author varchar (40) not null,
Time date not null,
Keyword varchar (20) not null,
Type int not null
) Charset = gb2312;
Then insert again. OK can be used to correctly insert Chinese characters and solve database problems;
2. re-insert Chinese characters on the webpage. It's still garbled !!
Here is my new news class:
Package newsbean;

Import java. util. vector;

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Import javax. SQL. datasource;

Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Action. actionmessage;
Import org. Apache. Struts. Action. actionmessages;
Import org. Apache. Struts. validator. dynavalidatorform;

/**
* @ Author sxwailyc
*
* To change the template of the type comment generated by todo, go
* Window-preferences-Java-code style-Code Template
*/
Public class newsaddaction extends action {

/* (Non-javadoc)
* @ See Org. apache. struts. action. action # execute (Org. apache. struts. action. actionmapping, org. apache. struts. action. actionform, javax. servlet. HTTP. httpservletrequest, javax. servlet. HTTP. httpservletresponse)
*/
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response) throws exception {
// Todo automatically generates method stubs
Dynavalidatorform newsform = (dynavalidatorform) form;
String title = (string) newsform. Get ("title ");
System. Out. println (title );
String content = (string) newsform. Get ("content ");
String author = (string) newsform. Get ("author ");
String keyword = (string) newsform. Get ("keyword ");
Integer newstype = (integer) newsform. Get ("newstype ");
Httpsession session = request. getsession ();
Vector newslist = new vector ();
Servletcontext context = servlet. getservletcontext ();
Datasource = (datasource) Context. getattribute (constants. performance_key );
DB = new dB (datasource );
String pageforward = NULL;
News news = new news ();
News. settitle (title );
News. setcontent (content );
News. setauthor (author );
News. setkeyword (keyword );
News. settype (newstype. intvalue ());
Actionmessages errors = new actionmessages ();
If (news. insert (db )){
Newslist = News. searchnewstitle (db );
Session. setattribute (constants. news_list_key, newslist );
Pageforward = "toadminmain ";
}
Else {
Errors. Add (actionmessages. global_message, new actionmessage ("errors. insertfail "));
If (! Errors. isempty ()){
Saveerrors (request, errors );
}
Pageforward = "towrong ";
}
DB. Close ();
Return Mapping. findforward (pageforward );

}
}
First, consider whether the data transmitted from the submitted form page is garbled?
Here we add a String title = (string) newsform. Get ("title"); system. Out. println (title );
Sure enough, the title printed here is garbled !!!!! Worried! That's the problem. It took more than a minute,
Finally, check the JSP page of the submitted form again. The original <form method = "Post"> post !!!
Garbled code occurs when the data is passed through the export form. Add method = "Post"; Re-system. Out. println ("title ");
Chinese is displayed correctly !!
3. I thought the Chinese characters should be correctly displayed. Add-> browse or Garbled text!
It should be the Page code validation problem!
Solution:
Write your own actionservlet
Myactionservlet
Public class myactionservlet extends actionservlet {

/* (Non-javadoc)
* @ See org. Apache. Struts. Action. actionservlet # process (javax. servlet. http. httpservletrequest, javax. servlet. http. httpservletresponse)
*/
Protected void process (httpservletrequest request, httpservletresponse response)
Throws ioexception, servletexception {
// Todo automatically generates method stubs
Request. setcharacterencoding ("gb2312"); // set the encoding to gb2312.
Response. setcharacterencoding ("gb2312 ");
System. Out. println ("set successfully ");
Super. Process (request, response );
}
}
Configure in Web. xml
<Servlet>
<Servlet-Name> actionservlet </servlet-Name>
<Servlet-class> newsbean. myactionservlet </servlet-class>
</Servlet>
4. Now, the test is okay. Add-> browse. It's garbled and re-check it again. No problem !!
Finally, think about whether it is the encoding of the data source. Set the database encoding in struts_config.xml.
<Set-Property = "url"
Value = "JDBC: mysql: // localhost: 3306/sample? Useunicode = true & amp; characterencoding = gb2312 "/>
Restart Tomcat
Add-> browse;
Haha, Chinese is displayed correctly!

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.