reprinted from: Http://www.linuxidc.com/Linux/2011-08/41685.htm
[Date: 2011-08-27] |
Source: Csdn Cloudyxuq |
|
1.IDE tools are available for myeclipse or eclipse
If it is eclipse need to download Tomcatt http://tomcat.apache.org and a plugin after unpacking Tomcatpluginv32.zip
2. To better understand how the server-side application executes, it is created manually. Project as follows
Create a Webroot folder under Servletdemo to create Web-inf to hold Lib and classes
The servlet is actually a Java file
To create the Myservlet class:
- Package Com.servlet;
- import java.io.IOException;
- import Java.io.PrintWriter;
- import javax.servlet.ServletException;
- import Javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**servlet is actually a Java file * /
- Public class Myservlet extends httpservlet {
- /**
- *
- */
- private static final long serialversionuid = 1L;
- //Call the construction method of the parent class
- Public Myservlet () {
- Super();
- }
- //Overriding the Doget () method of the parent class
- protected void doget (httpservletrequest req, HttpServletResponse resp)
- throws Servletexception, IOException {
- //Usually the GET request is forwarded to the POST request
- DoPost (REQ,RESP);
- }
- protected void doPost (httpservletrequest req, HttpServletResponse resp)
- throws Servletexception, IOException {
- //TODO auto-generated method stub
- //write response information for processing post requests
- PrintWriter Pw=resp.getwriter ();
- Pw.println ("This ismy fisrt Servlet");
- Pw.flush ();
- Pw.close ();
- }
- }
3. Build your servlet's configuration file with the Web. XML code:
- <? XML version =< Span class= "Attribute-value" > "1.0" encoding = "UTF-8" ?>
- <Web-app id="webapp_id" version="2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
- xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
- Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
- <!--adding servlet elements --
- <servlet>
- <!--servlet implementation name, can be arbitrary, but it's best to match your Servlet implementation class name --
- <servlet-name> Myservlet</servlet-name>
- <!--To specify the implementation class for the servlet--
- <servlet-class>com.servlet.MyServlet</servlet-class >
- <!--load-enabled (Load-on-startup sets the server load time >0 is loaded numerically, and if it is <0, it will only be loaded when the servlet is called)--
- <load-on-startup>1</load-on-startup>
- <!--display name --
- <display-name> first servlet</display-name>
- </servlet>
- <!--access servlet through a page, requires servlet mapping configuration-
- <servlet-mapping>
- <!--name must be the same as that in the servlet--
- <servlet-name> Myservlet</servlet-name>
- < when calling a servlet class in a!--page, the name can be arbitrarily taken, but required/-
- <url-pattern>/myfirstservlet</url-pattern>
- </servlet-mapping>
- <!--default page--
- <welcome-file-list>
- <!--can set up many pages, such as index.htm,index.html, etc.--
- <welcome-file>
- index.jsp
- </welcome-file>
- <welcome-file>
- Index.html
- </welcome-file>
- <welcome-file>
- default.jsp
- </welcome-file>
- </welcome-file-list>
- </web-app>
4. Create your own first index.jsp
- <html>
- <head>
- <Meta http-equiv= "content-type" Content="text/html; Charset=iso-8859-1 ">
- <title> my servlet</title>
- </head>
- <body>
- This is my index
- </Body>
- </html>
5. Configure the virtual directory in the Server.xml in the Conf file under the Tomcat folder to access it through IE and other browsers
Established between the
- <Host>
- .
- .
- .
- .
- .
- <!--Add virtual directory, docbase to real directory--
- <Context Path="/servletdemo" docBase="C:\Users\Cloudy\workspace\ Servletdemo\webroot " reloadable=" true "/>
- </Host>
6. Visit index and Myservletdemo separately
Servlet+tomcat to make the first Java application running on Tomcat