JSP basics of Resource Sharing

Source: Internet
Author: User
We have been studying JSP for a long time. We have summarized a little basic knowledge in the process and used it to share it with beginners until we finally made a small project, Pet Hospital, jcreater4.0, our simplest tool, was used to do this. Oh, it was only found in ecliplse that the development speed was so fast, but it also laid the foundation for us .... if you don't want to talk about it, send a summary of what you have learned to this point, and hope to bring convenience to beginners. At the same time, I hope that the experts will give you some advice .....

First, let's take a look at our JSP Course: The first part is Servlet technology, including servelt entry, session tracking, javamal and servlet. the second part is the JSP technology, including: JSP introduction, use of JSP scripts and commands, JSP implicit objects, standard actions in JavaBean and JSP, JSP expression language, custom tags, JSP User-Defined table signature library. the third part is the design pattern of the filter, including the design pattern of the filter and MVC.

I. servlet knowledge and Common Errors and rules.

1. The process of desktop running programs and Web applications is essentially the same-the process of request and response.
2. http protocol (Hypertext Transfer Protocol)
1). Stateless: there is no connection between multiple requests.
2). Used to send requests and response messages over the Internet
3). Use the port to receive and send messages. The default port is port 80.
Port: memory buffer (receiving data with multiple threads)
Windows: Port 0---65535
0---4096 indicates the system Port
3. What is Servlet?
Server let server-side applets.
A program written by the server to process requests sent by the client and respond to the client in Java.
Servlet only runs on the server

4. servlet details:
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. util .*;
Public class myservlet extends httpservlet
{
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = gb2312 ");
Request. setcharacterencoding ("gb2312 ");

Response. sendredirect ("login. jsp"); // redirect Method
Request. getparameter (""); // read client data

// The following methods are used for forwarding. Different from redirection, data is not lost during forwarding.
Servletcontext context = This. getservletcontext ();
Requestdispatcher dispatcher = context. getrequestdispatcher ("/welcome. jsp ");
Dispatcher. Forward (request, response );

... // The following method is included (rarely used)
Dispatcher. Include (request, response );

}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Doget (request, response );
}
}

(2). When you need to pass parameters to servlet, src = "Servlet? Name = ''";

5. When the servlet is executed, a download prompt is displayed:

(1). Likelihood 1: text/html; charset = GBK; the semicolon in the middle is written as a comma.
(2). Possibility 2: configuration information in XML is incorrect.
(3). Likelihood 3: for example, when defining a global variable, for example, content_typed will be placed in double quotation marks for future use.
(4). Possibility 4: When an error character is written in text/html and charset, the system will prompt you to download the character.

6. When executing the servlet, the following error occurs:

(1). Mostly xml configuration errors.
(2). a url error may occur in the servlet communication method.
(3). Form submission: Action path.
(4). A relatively mentally retarded error is marked as an error. Please check carefully.

7. the following error occurs during servlet execution:

(1) An error occurred in the Tomcat site.

8. When the servlet is executed, the following error occurs:

(1). When the servlet does not have the post submission method.

95. A 500 error occurs when the servlet is executed:

(1). Most of them are exceptions in servlet program code.

10. During execution, the retrieved data is null. You need to find the data according to the actual error message.

11. When the JSP page is executed, the 500 error message is displayed ....

Most of the JSP pages are compiled incorrectly during translation! Very serious error. You can follow the prompts to go back and find...

Ii. xml configuration

<! -- Configuration information in config, which needs to be configured in XML in servlet -->
<Servlet>
<Init-param>
<Param-Name> sess </param-Name>
<Param-class> com. Serv <param-class>
</Init-param>
<Servlet-Name> myservlet </servlet-Name>
<Servlet-class> com. myservetl </servlet-class>
</Servlet>

<! -- Ing myservlet -->
<Servlet-mapping>
<Servlet-Name> myservlet </servlet-Name>
<Servlet-class>/URL </servlet-class>
<Servlet-mapping>

<! -- Configuration information in Context -->
<Context-param>
<Param-Name> ses </param-Name>
<Param-class> com. Ser </param-class>
</Context-param>

Note: configuration information can only be read. config can be accessed in a single servlet, and context can be accessed globally.

III.
==================================== Session tracking Technical Summary ================ ================

User authorization.
Hide form fields
URL rewriting
Cookie usage
Bytes --------------------------------------------------------------------------------------------
1. Session: multiple requests and responses between the same client and server at the same time.

2. Session usage (important)

Httpsession session = request. getsession ();
Session. setattribute ("name", object); // Value
Session. getattribute (); // Value
Session. removeattributer ();

3. Cookie (class)
1). What is a cookie?
Cookie is a string that is supported by HTTP and can be permanently saved on the client. Write (hard disk ).
Each request will leave space for cookies in the response.
2). Usage:
Cookie = new cookie ("name", cookie); // the key and value of the cookie must be specified and must be a string.
Response. addcookie (cookie );
Cookie. setmaxage (3600); // in seconds.
 
// Read the cookie sent from the client. The returned value type is: Cookie array.
Request. getcookies ();
// Read the key and value cyclically.

Usage process: (1). Generate cookie, new cookie ("","")
(2). Set the lifecycle to 0 and setmaxage (seconds ).
(3). send to the client: Response. addcookie (cookie );

TIPS: (1) a website can write up to 20 cookies to a client.
(2) A client can receive a maximum of 300 cookies.

4. Relationship between session and COOKIE:
The session ID is transmitted between the client and the server as the cookie value.
Bytes --------------------------------------------------------------------------------------------

4. Principles for building entity beans:

/*
* 1. The Bean class is public.
* 2. The class member is private.
* 3. A parameter-free structure is required.
* 4. Set () and get () methods are available.
* 5. The method is named setxxx () or getxxx ().
*/

5. Implement Data encapsulation using MVC

This data encapsulation standard is summarized by MVC.

Required content: 1. servlet 2. JavaBean 3. operbean (dbconnection) 4.jsp

The servlet extracts the Foreground Data, encapsulates the data in JavaBean, instantiate operbean, and calls the method in operbean,
After passing the JavaBean object as a parameter and performing a series of operations, the returned data is stored in the arraylist or other sets, and the Set object is encapsulated in the session object, it is convenient to set the value at the front-end.

Servlet control:

........
Arraylist Lis = new arraylist ();

String name = request. getparameter ("username ");
 
JavaBean bean = new JavaBean ();
Bean. setname (name );
 
Operbean merge = new operbean ();
Lis = protocol. opermethod (bean );

Httpsession session = request. getsession ();
Session. setattribute ("list", Lis );

Operbean logic:
...
Public arraylist opermethod (JavaBean bean)
{
Arraylist Lis = new arraylist ();
String USR = bean. getname ("name ");

String SQL = "select * from student where name = '" + USR + "'";

Dbconnection DB = new dbconnection ();
... // Operate data services

Return Lis;
}

Front-end business:
...
Arraylist list = (arraylist) Session. getattribute ("lis ");
Iterator ite = List. iterator ();
While (ITE. harnext () // traverses the output
{
JavaBean bean = (JavaBean) ite. Next ();
.
// Obtain the bean Value
}

// You can perform operations such as "User Login", "Data addition, deletion, modification, and query", and "logout and login .....

All of the above JSP technologies are not related to struts and other frameworks. They are just simple MVC ideas. Only by understanding the above simple ideas can we further study these high-level aspects of the framework, I feel like this. After all, I am also a beginner. I still don't know much about it, and I am still learning it. If any of you have a good idea or a good learning method, please give me more advice!

 

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.