标签:
参考书《JSP Web 开发案例教程》
在jsp项目中的src文件下面创建HelloServletTest.java,代码如下,注意src的包名,我的包名是zhbit.com
package zhbit.com;
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; public class HelloServletTest extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Servlet</title></head>"); out.println("<body>"); out.println("你好,欢迎来到Servlet世界"); out.println("</body>"); out.println("</html>"); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ doGet(request, response); } }
修改web.xml文件,web.xml位置,web.xml修改内容,如下图
然后打开IE,在地址栏输入:http://localhost:8080/TestJsp/HelloServletTest
标签:
原文地址:http://www.cnblogs.com/xuqiulin/p/4423514.html