12.1第一个Servlet程序

发表日期:2015-02-15 21:20:08 | 来源: | | 浏览(741) 分类:JSP基础

项目目录下配置web.xml

 <servlet>

  <servlet-name>hello</servlet-name>//设一个servlet的名称

  <servlet-class>test.canquick.eniac.demo.ServletDemo</servlet-class>//对应的servlet.clss文件

 </servlet>

 <servlet-mapping>

  <servlet-name>hello</servlet-name>//设置对应配置好的servlet的名称

  <url-pattern>/helloServlet</url-pattern>//访问路径

  http://localhost:xx端口/helloServlet    就可以访问此servlet

 </servlet-mapping>


ServletDemo.java类:

package test.canquick.eniac.demo;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ServletDemo extends HttpServlet {

public void doGet(HttpServletRequest hsrq,HttpServletResponse hsrs) throws IOException{

hsrs.setCharacterEncoding("UTF-8");

PrintWriter out = hsrs.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");

out.println("<title>123456</title>");

out.println("</head>");

out.println("<body><h1>d标s题d</h1></body>");

out.println("</html>");

out.close();

}

}



集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。