TestServletConfig
原文链接:https://www.f2er.com/xml/297609.html
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServletConfig extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
ServletConfig config = this.getServletConfig();
String editor = config.getInitParameter("editor");
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>ServletConfig</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>本栏目编辑:" + editor + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
TestServletContext
xml信息:
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServletContext extends HttpServlet{
protected void doGet(HttpServletRequest request,IOException {
ServletContext context = this.getServletContext();
String webmaster = context.getInitParameter("webmaster");
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>ServletContext</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1 align=center>网站管理员:" + webmaster + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
xml信息:
<?xml version="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">
<display-name>
javaee</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>servlet.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/servlet/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>servlet.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/servlet/register</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>testSession</servlet-name>
<servlet-class>servlet.testSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testSession</servlet-name>
<url-pattern>/servlet/testsession</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>easyregister</servlet-name>
<servlet-class>servlet.easyregister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>easyregister</servlet-name>
<url-pattern>/servlet/easyregister</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TestServletConfig</servlet-name>
<servlet-class>servlet.TestServletConfig</servlet-class>
<init-param>
<param-name>editor</param-name>
<param-value>李鸿</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>TestServletConfig</servlet-name>
<url-pattern>/servlet/testservletconfig</url-pattern>
</servlet-mapping>
<context-param>
<param-name>webmaster</param-name>
<param-value>闻益舜</param-value>
</context-param>
<servlet>
<servlet-name>TestServletContext</servlet-name>
<servlet-class>servlet.TestServletContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServletContext</servlet-name>
<url-pattern>/servlet/testservletcontext</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>testForward</servlet-name>
<servlet-class>servlet.testForward</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testForward</servlet-name>
<url-pattern>/servlet/testforward</url-pattern>
</servlet-mapping>
</web-app>