Servlet for JavaWeb Learning (1) ---- configuration of MyEclipse and Tomcat, myeclipsejavaweb

Source: Internet
Author: User

Servlet for JavaWeb Learning (1) ---- configuration of MyEclipse and Tomcat, myeclipsejavaweb

[Statement]

Reprinted, but keep the original source of the article → _ →

Life One: http://www.cnblogs.com/smyhvae/

Source: http://www.cnblogs.com/smyhvae/p/4134921.html

Contact: smyhvae@163.com

 

[Development Environment]

Physical server version: Win 7 flagship edition (64-bit)

Jdk version: jdk1.8.0 _ 20

MyEclipse version: 10.7.1

Tomcat: apache-tomcat-7.0.57

1. Basic Servlet concepts:

Servlet is a dynamic web resource development technology provided by sun. It is essentially a java Applet. It can be added to the Servlet container for running.

  • Servlet Container: The environment that can run Servlet is called Servlet container. --- tomcat
  • Web Container: The environment that can run web applications is called web Container --- tomcat

Basic Servlet running mode:

Servlet does not have the main () method. They are controlled by another Java application (such as Tomcat). This Java application is called the Sevlet container, and Tomcat is such a container. We usually call Tomcat a Servlet container.

 

 2. Create a Web project using MyEclipse:

In actual development, you can directly create a Servlet File(Method 2), the Web. xml will be configured automatically.

NOTE 2: Tomcat built-in Servlet jar package.

1. Method 1: Create a class file

(1) create a new class file MyServlet. java that inherits the subclass HttpServlet:

MyServlet. java:

1 package com. vae. servlet; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 6 import javax. servlet. servletException; 7 import javax. servlet. http. httpServlet; 8 import javax. servlet. http. httpServletRequest; 9 import javax. servlet. http. listen; 10 11 // generally implement a Servlet. As long as it inherits the HttpServlet class, 12 public class MyServlet extends HttpServlet {13 @ Override14 protected void doGet (HttpServletRequest req, HttpServletResponse resp) 15 throws ServletException, IOException {16 // TODO Auto-generated method stub17 // super. doGet (req, resp); 18 // get the output stream (out object) of the client 19 PrintWriter out = resp. getWriter (); 20 out. write ("" Hello, Servlet ""); 21} 22 23 @ Override24 protected void doPost (HttpServletRequest req, HttpServletResponse resp) 25 throws ServletException, IOException {26 // TODO Auto-generated method stub27 // super. doPost (req, resp); 28} 29}

In the code above, if a get request is used, the doGet method can be rewritten.Note that you need to comment out the 17th lines of code (the method of the parent class).

In fact, the doPost and doGet methods are actually called inside the service () method of the Servlet class. Tomcat has given the request and response parameters of the 14 rows to new.

Row 19: PrintWriter is a character print stream. The output source of this character stream is a page (output to the client ).

(2) Configure Web. xml and configure the Servlet's external access path (URL ):

Since this Servlet is started by a Web container, it needs to be configured in Web. xml. In this way, Tomcat will automatically load this file at startup.

The browser must use a url to access the Web. Therefore, you also need to configure a ing path through the tag <servlet-mapping> in Web. xml, and the alias name and path url are also attached.

Open web. xml and add some code.

Web. xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <display-name> </display-name> 7 <welcome-file-list> 8 <welcome-file> index. jsp </welcome-file> 9 </welcome-file-list> 1011 <! -- Configure a servlet --> 12 <servlet> 13 <servlet-name> helloServlet </servlet-name> 14 <servlet-class> com. vae. servlet. myServlet </servlet-class> 15 </servlet> 16 17 <! -- Configure the URL path of a servlet ing --> 18 <servlet-mapping> 19 <servlet-name> helloServlet </servlet-name> 20 <url-pattern>/hello </url -pattern> 21 </servlet-mapping>22 23 </web-app>

Lines 11 to 21 are the code I added.

12 to 15 rows: Web. xml configuration. A name maps to a class, and the alias can be retrieved at will to facilitate the entire web. the Servlet is referenced in xml (13 rows), but the class name must be the defined Servlet, including the package name (14 rows ).

Line 18 to line 21: configure the url to allow the browser to access the Web end through this url. The name alias of line 19 must be the same as that of line 13. line 20 "/hello" is the url path we set.

In fact, the alias for 13 rows is found based on the URLs of 20 rows, and then the Servlet class for 14 rows is found.

Run the program as follows:

Be sure to delete methods that inherit the parent class in MyServlet. java code: super. doGet (req, resp);, super. doPost (req, resp );(Lines 17 and 27). Otherwise, the following error page appears:

1 package com. vae. servlet; 2 3 import java. io. IOException; 4 import java. util. date; 5 6 import javax. servlet. servletException; 7 import javax. servlet. http. httpServlet; 8 import javax. servlet. http. httpServletRequest; 9 import javax. servlet. http. httpServletResponse; 10 11 public class MyServlet extends HttpServlet {12 13 public void doGet (HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException {15 // display the current time 16 response. getWriter (). write ("now time:" + new Date (). toLocaleString (); 17 18} 19 20 public void doPost (HttpServletRequest request, HttpServletResponse response) 21 throws ServletException, IOException {22 doGet (request, response); 23 24} 25 26}

Enter the url on the webpage. The effect is as follows:

 

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.