Create a simple servlet controller

Source: Internet
Author: User

Brief Introduction:

Build a simple servlet framework, Eclipse Java EE IDE for WEB developers


steps:

1. Create a new Web project = "WebProj2.5


2. Add servlet2.5 package, I use the Ivy plugin, modify its Ivy.xml file can automatically add the corresponding file

(Ivy Plug-ins Add an article in a reference configuration, or you can manually add to the package without using the Ivy plugin)

Documents are as follows

Ivy.xml

<?xml version= "1.0" encoding= "iso-8859-1"?> <!--licensed to the Apache Software Foundation (ASF) under one  or more contributor license agreements.  The NOTICE file distributed with this work for additional information regarding copyright. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License");  You are not to use this file except in compliance with the License.  Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable Agreed to writing, software distributed under the License was distributed on ' as is ' basis, without WAR  Ranties or CONDITIONS of any KIND, either express OR implied.    
The License for the specific language governing permissions and limitations under the License. --> <ivy-module version= "2.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xsi:nonamespaceschemaloc Ation= "Http://ant.apAche.org/ivy/schemas/ivy.xsd "> <info organisation=" "module=" Webproj "status=" Integratio n "> </info> <dependencies> <dependency org=" Javax.servlet "name=" Servlet-api "rev=" 2.5 "/>"
 ;/dependencies> </ivy-module>

The plugin will then automatically import the corresponding servlet package

As shown in figure:


3. Create a new servlet package: Webproj.servlet

Create a new servlet class in it: Testservlet

Testservlet.java

Package webproj.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{
	private static final long serialversionuid = 1L;

	public void doget (HttpServletRequest request, httpservletresponse response)
	throws Servletexception, IOException {
		Response.setcontenttype ("Text/html;charset=utf-8");
		Response.getwriter (). Append ("Testservlet works");
	}


4. To enable the servlet to request the appropriate server, you also need to configure the XML

Url-pattern is the entrance to the servlet.

Xml

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi=
"Http://www.w3.org/2001/XMLSchema-instance" "Xmlns=" Http://java.sun.com/xml/ns/javaee "xmlns:web=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 >
  <display-name>WebProj2.5</display-name>
  
	<servlet>
		< Servlet-name>testservlet</servlet-name>
		<servlet-class>webproj.servlet.testservlet</ servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>testservlet</ servlet-name>
		<url-pattern>/Test</url-pattern>
	</servlet-mapping>
</ Web-app>

5. The whole document looks like



Last run in server (Tomcat 6.0.32 version)

My port has been modified, it is 8080 without modification.
























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.