欢迎页面:
代码详解:
web.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"
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"> <!-- 欢迎页面的设置 -->
<!-- <welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list> --> <!-- <welcome-file-list>
<welcome-file>html/welcome.html</welcome-file>
<welcome-file>login.html</welcome-file>
</welcome-file-list> --> <welcome-file-list>
<welcome-file>system/welcome</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/system/welcome</url-pattern>
</servlet-mapping> </web-app>
WelcomeServlet:
package com.bjpowernode.javaweb.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class WelcomeServlet implements Servlet{ @Override
public void destroy() { } @Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
} @Override
public String getServletInfo() {
return null;
} @Override
public void init(ServletConfig arg0) throws ServletException { } @Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); out.print("<html> ");
out.print(" <head> ");
out.print(" <title>welcome</title> ");
out.print(" </head> ");
out.print(" <body> ");
out.print(" 这是Servlet,也可以作为欢迎页面<br> ");
out.print(" </body> ");
out.print("</html> "); } }
HTTP状态码: