Introduction to Java Servlet Learning

Source: Internet
Author: User

A: What is a servlet?

servlet: is a server-side program written by Java. Its main function is to interactively browse and modify the data, my understanding is:theservlet accepts requests from the client, and then forwards them to the business logic processing, which itself does not do the relevant business logic processing, similar to the foreground framework MVC Controller in the .

in narrow sense:servlet refers to the Java language implementation of an interface, that is, the httpservlet interface;

Broadly speaking:aservlet is a class that implements the servlet interface.

In simple terms, a servlet is a class in the Java programming language.



Second: Through the servlet to achieve additions and deletions to search

If you simply use the servlet to implement additions and deletions, it is simply too simple, the following formally entered our introductory demo. Its main content is a simple form form, the content is a name, and then to change its additions and deletions.

1 · Open MyEclipse and create a new Web project:


2 · Create a new index.jsp page under the Webroot folder:

Right-click Webroot folder--new--other--web--jsp (Basic templates)

3 · After the new JSP page succeeds, change the encoding format first:

Menu bar--window--preferences--myeclipse--files and editors--jsp, change encoding to content:


4 · Add code on the index.jsp page:

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
5 • Right-click the src folder: Select New--package, name package: Com.bjpowernode.servlet

6 · Right-click the new package, select New--class, and then create a new servlet class: Testservlet

7 · Fill in the code in the Servlet class:

Note: The Servlet class inherits the HttpServlet class and overrides the Doget and Dopost methods in it, and the choice of this inheritance method depends on how the JSP is requested. Right-click the Testservlet class and select Source--override/implement Methods

Pop-up dialog box, select the Dopost and Doget methods. The code is as follows:

Package Com.bjpowernode.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Testservlet extends HttpServlet {@Overrideprotected void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//Get parameters, only take out parameters, Passed to the business logic string username = Request.getparameter ("username"); Usermanager Usermanager = new Usermanager (); String URL = Request.getrequesturl (). toString (); System.out.println ("url=" + URL); String path = url.substring (Url.indexof ("/", 3), Url.indexof (".")); System.out.println ("path=" + path), if (Path.contains ("add")) {Usermanager.add (username);//forwarding and redirection: If the data is passed back it is forwarded, If the parameter is not brought back, it is redirected, which is the forward request.getrequestdispatcher ("/add_success.jsp"). Forward (request, response); else if (path.contains ("delete")) {Usermanager.delete (username); Request.getrequestdispatcher ("/delete_success.jsp "). Forward (request, response);}} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doget (request,response);}}

8 · Change Serlvet class just accept the parameters from the JSP, and then pass it to the business logic processing, so, add Class: Usermanager, right click on our newly added package, then New--class. The class name is: Usermanager

9 · Add code to the Usermanager class: mainly dealing with business logic processing.

Package Com.bjpowernode.servlet;public class Usermanager {//Increase public void Add (String username) {System.out.println (" Usermanager.add ()---->> username "+ username);} Delete public void Delete (String username) {System.out.println ("usermanager.delete ()---->> username" + username);} Query public void query (String username) {System.out.println ("usermanager.query ()---->> username" + username);}}
10 · Configure the Web. XML configuration file, under the Webroot--web-inf folder, Web. XML, configured as follows:

<?xml Version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name></display-name> <welcome-file-list > <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-nam E>testservlet</servlet-name> <servlet-class>com.bjpowernode.servlet.testservlet</ servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>*.do</url-pattern> <!--similar to Fifter, filters, files with the suffix. Do will be intercepted by the servlet--><!--< Url-pattern>*.do</url-pattern>--</servlet-mapping> </web-app> 
Basic code This is done, and then the Web project is deployed, and the deployment is simple,

1. Select the menu bar--window--preferences,

After the popup box appears, find Myeclipse--servers--tomcat in turn and select the Tomcat version. I chose tomcat7.0, then click Browse and select the Tomcat local path. Then change "Tomcat 7.x Server" to enable to allow tomcat7.0 to be used in the project. :


2 · In the left column tomcat7.x, select the JDK, and on the right, select the local JDK.


3 • Add the item to the Tomcat server. Select the icon for the menu bar:


When the popup box appears, click Add to add the item to tomcat.

4 · Then it's OK. Tomcat 7.x appears in the Servers window, and your item in the drop-down list indicates OK. :


OK, done, now we open the browser, enter: Http://localhost:8080/test_servlet, Access can. Interface appears:

Now we type in the text box: servlet test, click Submit, and then appear:

That means we are successful! This is through the servlet implementation of the additions and deletions, the following we illustrate the basic implementation of the servlet process.



Three: servlet workflow

       1 Web client issues a The request is a regular request: similar to *.action;. do;

             2 web.xml configuration file results in the request, through web.xml configuration file mapping url Find the mapped servlet class

             3 • servlet jsp doget () or dopost () dohead (), DoPut (), Dotrace (), DoDelete (), dooptions () servlet Interface package Good method, implement class implementation can.

4 · servlet the corresponding method of the class receives the parameters sent by the client and passes them to the business logic layer action to process and returns the parameters received from the business logic layer to the client by forwarding or redirection.

5 • The client receives a return value display.

The workflow is represented by a timing diagram:





Four: summary


Front-end Framework MVC as we all know, by comparing the two to the servlet, we can see that the servlet acts as a "C" in MVC, in MVC, the controller accepts parameters from view views and then passes them to the background processing. The background is processed and returned to Controller,controller and returned to the view, presented to the user. The same is true for Servlets. Accept the parameters from the page JSP, and then the Servlet class accepts and passes to the background business logic processing class Usermanager,usermanager returns to the Servlet class after processing, and the servlet returns to the page JSP and renders it to the user. To describe the relationship between them in a diagram:


This is the whole. The initial study of the servlet, understanding is still left on the surface, with the continuous study of learning, and then understand.



Introduction to Java Servlet Learning

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.