Summary of problems encountered by new users in JSP website development

Source: Internet
Author: User
Tags url forwarding

1. Jump to and pass the parameter by responding to the onclick method, and use reque. getparameter ("user") on another page to get the value of this parameter.
Onclick = "location. Replace ('userprivate. jsp? User = <% = Rs. getstring ("gid") %> ')"
Here, RS is the return value of the database I am searching.

 

 

2. A confirmation and cancellation prompt box is displayed using JavaScript:

<SCRIPT type = "text/JavaScript">
Function Test (){
VaR K = Window. Confirm ("are you sure you want to modify or delete this record ");
If (k ){
Event. returnvalue = true;
}
Else {
Event. returnvalue = false;
}
} </SCRIPT>

<TD> <a href = "groupdelete. jsp? Gdelete = <% = Rs. getstring ("gid") %> "onclick =" test () "> Delete </a> </TD>

When you click "delete" in the expression, a dialog box is displayed. When you select "OK", the link to which href is directed is displayed. At this time, test () returns true, test () When you click "cancel ()
False is returned, so the page will not jump. Gdelete can be any string, such as gdelet = "hello ".
In <a href = "/Good. jsp"

 

3. Javascript obtains <form name ="Registerform"...> <Input name ="Password"...> <Input type = "Submit" name = "Submit" value = "Submit" onclick = "test ()"/> </form>:

Registerform is the form name, password is the name of input, and value is the value corresponding to input,
Test () is called in the last submit button. If test returns true, the form is submitted. Otherwise, a prompt is displayed, and the form is not submitted.
<SCRIPT type = "text/JavaScript">
Function Test (){

If (document.Registerform. Password. value = "")
{
Alert ("the password cannot be blank! ");
Event. returnvalue = false;
Return;
}
If (document. registerform. Password. value! = Document. registerform. confirmpassword. value = "")
{
Alert ("the two passwords are different! ");
Event. returnvalue = false;
Return;
}
}
</SCRIPT>

 

4. Add a column to the database: alter table userorder add personnum int;

 

> <U> <a href = "storeuserorder. jsp? User = <% = request. getparameter ("user") %>
& Starttime = <% = request. getparameter ("starttime") %>
& Endtime = <% = request. getparameter ("endtime") %>
& Personnum = <% = request. getparameter ("personnum") %>
& Totalmoney = <% = totalprice %>
& Textarea = <%> "> confirm and submit the order </a> </u>

 

5.

There are two types of pass-through between JSP and Servlet: JSP-> servlet, servlet-> JSP.
Use the Object Request and Session (regardless of Application) to complete the value transfer.

I. jsp-> Servlet
The JSP page has three methods to pass values to the servlet: Form, URL, and others.

<! -- JSP page -->
...
<% ......
Session. setattribute ("testsession", "Hello session ");
Reqeust. setattribute ("testrequest", "Hello request ");
%>
<A href = "jspservlet? Action = toservlet "> click me </a>
<Form action = "jspservlet? Action = toservlet "method =" Post "name =" form ">
<Input name = "username" type = "test"/>
<Input type = "Submit" value = "Submit">
</Form>
...

 

5.1 for the content of the Form on the JSP page, such as the <input> tag, you can use request. getparameter ("username"); In the servlet to obtain the content.
5.2. url: for example, the <A> tag's href attribute and the <form> tag's action attribute value "jspservlet? Action = toservlet ", the request is also used in servlet. getparameter ("action") is obtained. Note that the URL and Servlet must be in the web. the path of the <URL-pattern> label in XML corresponds. This part will be mentioned later.
5.3 Java fragment code. servlet can only receive the content of session. setattribute ("testsession", "Hello session"), but cannot receive the content of the request. Use request. getsession (). getattribute ("testsession") in servlet to get the session content.

6. servlet
6.1 for servlet, you should first mention its registration content in Web. XML, as shown in

<Servlet-Name> jspservlet1 </servlet-Name>
<Servlet-class> com. Demo. jspservletdemo </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> jspservlet1 </servlet-Name>
<URL-pattern>/jspservlet </url-pattern>
</Servlet-mapping>

<Servlet-Name> jspservlet2 </servlet-Name>
<Servlet-class> com. Demo. jspservletdemo </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> jspservlet2 </servlet-Name>
<URL-pattern>/admin/jspservlet </url-pattern>
</Servlet-mapping>

If the project name is jsp2servlet, the context of the project root directory is/jsp2servlet, which is displayed in the address bar

Http: // localhost: 8080/jsp2servlet /;

The project root directory contains the Admin directory. The corresponding context is/admin/jsp2servlet, which is displayed in the address bar

Http: // localhost: 8080/jsp2servlet/admin,

JSP files in both directories want to be transferred to the com. Demo. jspservletdemo class for processing. At this time, the URL must be registered twice in Web. xml.
1) In jspservlet1 of the JSP page under http: // localhost: 8080/jsp2servlet/directory, the URL should be written as "jspservlet"
2) Access jspservlet2 on the JSP page in the http: // localhost: 8080/jsp2servlet/admin/directory. The URL should be written as "admin/jspservlet"

6.2 directly use the request object in the servlet to obtain the sent request content. Use request. getsession () to obtain the session object and obtain the session content.

Here, the request. getsession () parameter is of the boolean type. This method can be understood:

A session can be considered to correspond to one session for each ie process (two sessions can be created for each new ie process). getsession returns the session object of the current user. The parameter differences are as follows:
If the parameter is true (default), a new session object is created if the "current user's session object" is blank (when the first access is made;
If the parameter is false, if the "current user's session object" is null, null is returned (that is, the session object is not automatically created ).

This method can be used to determine whether the session has expired, as shown below:

If (request. getsession (false) = NULL)
System. Out. println ("session has been invalidated! ");
Else
System. Out. println ("session is active! ");
7. servlet-> JSP
Two methods are available for transferring from servlet to JSP: redirection and URL forwarding.

7.1. Redirect: the redirection of the path, and the content and URL change. The request parameter (the session parameter is acceptable) is not allowed, that is, the request object cannot be sent to the next page using the setattribute method in the servlet. Use the response. sendredirect (URL) method in the servlet. Note that the URL here does not contain a slash/, such as response. sendredirect ("test. jsp").

7.2. url Forwarding (forward): It is a page Jump. The page content changes and the URL remains unchanged. It can contain request and session parameters. Use getservletconfig (). getservletcontext (). getrequestdispatcher (URL). Forward (request, response) in servlet ). The URL here requires a slash (/), such as getservletconfig (). getservletcontext (). getrequestdispatcher ("/test. jsp"). Forward (request,
Response)
Use request. setattribute ("sequence", sequence); set the value of sequence. In JSP, order_num = request. getattribute ("sequence "). tostring (); to obtain the corresponding value.

8. Never use JSP again <%! %> To declare global variables, because these variables will not be reset when you jump to the next page, so that they will accumulate.
9. The path of the JSP file made with Dreamweaver may exist, as long as you add the following to the JSP file:
(1) Add at the beginning of JSP
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
(2) Add <base href = "<% = basepath %>"> to the JSP head.

10. because the servlet is only loaded once at run time, the database connection operation cannot be defined as static. You must re-connect to the database when no operation is performed, otherwise, an error occurs after the first correct operation.
11. the result set after the database query stores the index of the corresponding item. The index cannot delete the row or close the link to the database before the corresponding item value is retrieved, otherwise, opration not allowed after resultset have been closed will appear;

 

12. Import and export MySQL database tables:
1. Data Import:
Enter the bin in the MySQL installation directory from cmd, and enter the following command to export the table:
Mysqldump-u root-P test-D> D:/test. SQL where test is the name of the database to be exported. D:/test. SQL: location and file name of the exported file
2. Data export:
Enter the following command to import the table (you only need to execute this statement)
Mysql-u root-P test <D:/test. SQL where test is the name of the database to be imported. D:/test. SQL is the position and file name of the import file.

These are the solutions to the main problems I encountered when developing background processing programs for my website some time ago. I hope they will be helpful to new users.

 

 

QQ: 772319357

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.