Java Web-----servelet

Source: Internet
Author: User

1, define servlet;

2,servlet life cycle;

3,httpservlet common methods;

4,servlet Common interface;

One, the servlet defines:

The servlet is a Java class that inherits the HttpServlet class and is created directly with the development tools

A servlet can receive a jump from a page, logically processing the action of a,<form> form

The URL of the servlet should be the same project under the ". /servletname ".

Second, the servlet life cycle:

1, generate servlet, load into servlet engine, initialize by calling Init () method;

2, processing client requests by providing a response method (Doget, doPost, service);

3, call the Distroy () method to destroy.

Third, servelet the usual way to handle clients:

1,doget () Handles a form that is submitted by a Get method, and also handles direct access to the browser.

Set output content format and encoding
Response.setcontenttype ("text/html; Charset=utf-8 ");

Set the encoding format for receive parameters
Request.setcharacterencoding ("Utf-8");

Receive parameters
String uname = Request.getparameter ("uname");

Get PrintWriter output content to a Web page

PrintWriter out = Response.getwriter ();

User Login Example:

  

(1. Front page userform.jsp)

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >View Code

(2.servlet receive submitted data)

 PackageCom.javaweb.servelet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class Dogetdemo*/@WebServlet ("/dogetdemo") Public classDogetdemoextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /**     * @seeHttpservlet#httpservlet ()*/     PublicDogetdemo () {Super(); //TODO auto-generated Constructor stub    }    /**     * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stub//DoPost (request, response);    }    /**     * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stub//Set output content format and encodingResponse.setcontenttype ("text/html; Charset=utf-8 "); PrintWriter out=Response.getwriter (); //set the encoding format for receive parametersRequest.setcharacterencoding ("Utf-8"); //Receive ParametersString uname = Request.getparameter ("uname"); String Password= Request.getparameter ("Password"); //Output ResultsOut.println ("); Out.println ("<body>"); Out.println ("User:" +uname); Out.println ("<br> Password:" +password); Out.println ("</body>"); Out.println ("); }}
View Code

2,dopost () handles forms submitted by post, and does not handle direct access to the browser.

The code is the same as doget ().

3,service () Handles get, post-submitted forms, and direct access to the client's browser.

Four, servlet common interface:

1,httpservletrequest the built-in objects corresponding to the JSP request;

2,httpservletresponse corresponds to the built-in object response of JSP;

3,httpsession similar to the JSP built-in object session, must be obtained through the request of the getsession ();

4,servletcontext built-in object application, similar to JSP, must be obtained through HttpServlet's Getservletcontext ()

ServletContext application = Getservletcontext ();

Java Web-----servelet

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.