Servlet入门(第一个Servlet的Web程序)

时间:2023-03-09 22:49:32
Servlet入门(第一个Servlet的Web程序)

新建maven项目,注意项目的类型

Servlet入门(第一个Servlet的Web程序)

project名为ServletExample

Servlet入门(第一个Servlet的Web程序)

点击Finish。建立maven项目完毕例如以下

Servlet入门(第一个Servlet的Web程序)

生成后的文件夹没有java源代码文件夹。依照maven的约定,还要新建src/main/java的源代码文件夹。选中项目,点击鼠标右键

Servlet入门(第一个Servlet的Web程序)

不知道为什么,新建src/main/java文件夹时。eclipse报这个文件夹已经存在。

没办法,先建立src/main/java2。然后再重构为src/main/java

Servlet入门(第一个Servlet的Web程序)

Servlet入门(第一个Servlet的Web程序)

Servlet入门(第一个Servlet的Web程序)

配置maven的构建文件pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<packaging>war</packaging>

<name>ServletExample</name>

<groupId>com.ydoing</groupId>

<artifactId>ServletExample</artifactId>

<version>0.0.1-SNAPSHOT</version>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>

</plugin>

</plugins>

</build>

<dependencies>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.0.1</version>

</dependency>

</dependencies>

</project>

更新maven项目

Servlet入门(第一个Servlet的Web程序)

新增HelloServlet

Servlet入门(第一个Servlet的Web程序)

代码例如以下,该类核心代码是doGet,作用是向client返回“Hello, world!”

package com.ydoing.helloapp;

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;

/**

* Servletimplementation class HelloServlet

*/

publicclassHelloServletextendsHttpServlet{

privatestaticfinallongserialVersionUID
=1L;

/**

* @see HttpServlet#HttpServlet()

*/

publicHelloServlet(){

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequestrequest, HttpServletResponse response)

*/

protectedvoid doGet(HttpServletRequest
request,HttpServletResponse response)throwsServletException,IOException{

// TODO Auto-generated method stub

response.setContentType("text/html");

PrintWriter out
= response.getWriter();

String msg
="Hello, world!";

out.println("<h1>"+
msg +"</h1>");

}

/**

* @see HttpServlet#doPost(HttpServletRequestrequest, HttpServletResponse response)

*/

protectedvoid doPost(HttpServletRequest
request,HttpServletResponse response)throwsServletException,IOException{

// TODO Auto-generated method stub

}

}

改动web.xml文件。作用是将URI映射到指定的Servlet上

<?

xml version="1.0" encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID"version="3.0">

<servlet>

<servlet-name>HelloServlet</servlet-name>

<servlet-class>com.ydoing.helloapp.HelloServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>HelloServlet</servlet-name>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

</web-app>

执行在Tomcat上

Servlet入门(第一个Servlet的Web程序)

执行结果例如以下

Servlet入门(第一个Servlet的Web程序)