1. share data between different pages or users
Share data between different pages for the same user
* Save the data in the session;
1) Scope = "session" of JavaBean ";
2) setattribute () and getattribute () of the session ();
* Cookie;
1) New cookie () and response. addcookie ();
* Use an implicit form;
2) <input type = "hidden" name = "" value = "/>;
* Use the servletcontext object;
1) Use the getservletcontext () method on the JSP page to obtain servletcontext (), that is, application;
2) pagecontext. setattribute (string name, object value, int scope );
* Use the application object;
1) application. setattribute ();
* Through the file system or database;
2. share data between different users
* Use the servletcontext object;
* Use the application object;
* Through the file system or database;
Getservletconfig ()
During servlet initialization, the container passes in a servletconfig object and stores it in the servlet instance,
This object allows access to two items: the initialization parameter and the servletcontext object. The former is usually specified by the container in the file,
Allows you to pass scheduling information to sevrlet at runtime, such as getservletconfig (). getinitparameter ("debug ")
The latter provides container information for the servlet. This method allows the servlet to obtain the object and configuration information at any time.
Getservletcontext ()
A servlet can use the getservletcontext () method to obtain the servletcontext of the Web application.
That is, some methods of getservletcontext are used to obtain some values.
For example, getservletcontext (). getrealpath ("/") is used to obtain the absolute path of the system.
Getservletcontext (). getresource ("WEB-INF/config. xml") to get the content of the XML file
3. Create error page
1) Use errorpage and iserrorpage;
2) Use
<Error-page>
<Error-code> 404 </error-code>
<Location>/pagenotfound.html </location>
</Error-page>
<Error-page>
<Error-code> 500 </error-code>
<Location>/internalerror.html </location>
</Error-page>
<Error-page>
<Error-type> JAVA. Lang. numberformatexception </error-type>
<Location>/numberformatexception, HTML </location>
</Error-page>
4. Internationalization i18n)
1) response. setheader ("content-language", "es"); sets the language to be displayed in HTML.
JDK native2ascii tool, can be converted into Chinese characters and other languages into UTF-8;
5. garbled Problem
1) gb18030> GBK> gb2312;
2) <% @ page contenttype = "text/html; charset = gb18030" %>
Request. setcharacterencoding ("gb18030 ");
3)
<%! String trans (string Chi)
{
String result = NULL;
Byte temp [];
Try
{
Temp = Chi. getbytes ("iso-8859-1 ");
Result = new string (temp );
}
Catch (unsupportedencodingexception E)
{
System. Out. println (E. tostring ());
}
Return result;
}
%>
6. jsp file operations
1) Use servletcontext
Inputstream in = getservletcontext (). getresourceasstream ("/file.txt ");
Int temp = 0;
String file = "";
While (temp = in. Read ())! =-1 ){
File + = (char) temp;
}
In. Close ();
2) use bufferedreader with buffer
Bufferedreader buffer = new buferedreader (New inputstreamreader (New bufferedinputstream (in ));
While (buffer. Readline ()! = NULL)
3)
Bufferedreader = new bufferedreader (New filereader ("C:/as.txt "));
4) write files
Printwriter writer = new printwriter (New bufferedwriter (New filereader ("C:/as.txt ")));
Filewriter ("C:/asd.txt", true );
7. upload files
1) The method must be post and enctype = "multipart/form-Data"
<Form action = "" method = "Post" enctype = "multipart/form-Data"/>
<Input type = "file" name = "file2" size = "20">
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<% @ Page import = "com. jspsmart. Upload. *" %>
<JSP: usebean id = "mysmartupload" Scope = "page" class = "com. jspsmart. Upload. smartupload"/>
<HTML>
<Head>
<Title> upload an attachment </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<Center> uploading file...
<%
// Upload an attachment
Try
{
Mysmartupload. initialize (pagecontext );
Mysmartupload. Service (request, response );
Mysmartupload. Upload ();
String fn = mysmartupload. getfiles (). GetFile (0). getfilename ();
Mysmartupload. Save ("upload/"); // the directory in which the file is saved is upload
Out. println ("the file has been successfully uploaded. Please check <a href = upload/" + FN + "> here </a> to see if the file has been uploaded successfully ");
}
Catch (exception E)
{
E. printstacktrace ();
}
%>
<A href?fileupload.html> re-upload </a>
</Body>
</Html>