Jsp & amp; Servelet Study Notes (1)

Source: Internet
Author: User

1.1 write a servelet Program
1.2 write a jsp program
1.3 compile a Servlet Program
1.4 Package A Servlets and Jsps Program
1.5 build a deployment descriptor.
 
 
1.1 write a servelet Program
 
Problem
You want to write a servlet as a web application
Solution
Write a java class that inherits from javax. servlet. http. HttpServlet. First, you must import a class package servlet. jar. You will need the class library to compile this servlet program.
Discussion
A servlet program is a java class used to respond to the dynamic content of client requests over the network. If you are familiar with CGI programs, java-based servlets can completely replace CGI programs. It is usually called a web component. When a servlet program is executed in a runtime environment, a servles container such as tomcat and bea weblogic is provided.
Note: A web Container can be
Servlets is installed in a web container as part of a web application. This application will contain WEB resources. Such as HTML pages, images, multimedia clips, servlets, jsps, xml configuration files, java runtime classes, and class libraries. When a web application is deployed in a web container, this container will generate and load the java servlet class instance to the JVM to process servlet requests.
All servlets inherit the javax. servlet. servlet interface. Programmers who develop Web applications write a servlet
Inherited from javax. servlet. http. HttpServlet, an abstract class implements the Servlet interface and is specially designed to process HTTP requests.
When a web Container generates a servlet instance, the basic sequence is as follows:
1. The servlet container first calls the servlet's init () method, which will initialize a resource for the servlet to use. For example, a logger. The init () method is called only once throughout the servlet lifecycle.
2. The init () method initializes an object that inherits the java. servlet. ServletConfig interface. This object enables the servlet to initialize parameters declared in the deployment descriptor. ServletConfig also gives servlet the right to use a javax. servlet. servletContext object. With this object, servlet can record information, dispatch requests to other web components, and be able to use other web resources on the same application.
3. The servlet container calls the servlet service () method to respond to some servlet requests. According to HttpServlets, service () automatically calls the appropriate HTTP method to process the request by calling the servlet doGet () or doPost () method. In a few examples, the user sends a Post request. At this time, the servlet responds to this request by executing the doPost () method.
4. When two main HttpServlet doPost () and doGet () methods are called, the servlet container will generate javax .. servlet. http. httpServletRequest and HttpServletResponse objects and pass them as parameters to these request processing methods.
Http://www.knowsky.com/
5. Manage the lifecycle of a servlet, or determine the time when the servlet instance processes the request on the Java VM. When a servlet container starts to remove a servlet, it will call the servlet's destroy () method to release all resources, such as a database connection.
Example A typical HttpServlet
 
Package com. mydev;
Import java. io. IOException; import java. io. PRintWriter;
Import java. util. Enumeration; import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
// The HttpServlet interface must be inherited.
Public class FirstServlet extends HttpServlet ...{
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, java. io. IOException ...{
// Set the MIME type of the response, "text/html"
Response. setContentType ("text/html ");
// Use a PrintWriter to send text data to the client who has requested
// Servlet
Java. io. PrintWriter out = response. getWriter ();
// Begin indexing ing the HTML content out. println ("Out. println ("<title> Help Page </title> Out. println ("// Make sure method = "post" so that the servlet service method
// Calldopost in the response to this form submit
Out. println ("<form method =" post "action =" "+ request. getContextPath () +"/firstservlet "> ");
Out. println ("<table border =" 0 "> <tr> <td valign =" top "> ");
Out. println ("Your first name: </td> <td valign =" top "> ");
Out. println ("<input type =" text "name =" firstname "size =" 20 "> ");
Out. println ("</td> </tr> <td valign =" top "> ");
Out. println ("Your last name: </td> <td valign =" top "> ");
Out. println ("<input type =" text "name =" lastname "size =" 20 "> ");
Out. println ("</td> </tr> <td valign =" top "> ");
Out. println ("Your email: </td> <td valign =" top "> ");
Out. println ("<input type =" text "name =" email "size =" 20 "> ");
Out. println ("</td> </tr> <td valign =" top "> ");
Out. println ("<input type =" submit "value =" Submit Info "> </td> </tr> ");
Out. println ("</table> </form>"); out. println ("</body> }
// DoGet
Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, java. io. IOException ...{
// Display the parameter names and values
Enumeration paramNames = request. getParameterNames ();
String parName;
// This will hold the name of the parameter
Boolean emptyEnum = false;
If (! ParamNames. hasMoreElements ())
EmptyEnum = true;
// Set the MIME type of the response, "text/html"
Response. setContentType ("text/html ");
// Use a PrintWriter to send text data to the client
Java. io. PrintWriter out = response. getWriter ();
// Begin processing ing the HTML content
Out. println ("Out. println ("<title> Submitted Parameters </title> If (emptyEnum )...{

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.