教程2:简单servlet程序实现

1工程src文件夹,new java package,com.aa.servlet

2new java class

~~~~~~~~~~~~~~~~~~~~~~~~~

package com.hyh.servlet;
import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");

PrintWriter out=resp.getWriter();
out.println("<html><head><title>Hello world!</title></head>");
out.println("<body>hahaha hello world</body></html>");
out.flush();


}
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~

修改web.xml

<servlet>
 <servlet-name>Helloworld</servlet-name>
 <servlet-class>com.hyh.servlet.HelloServlet</servlet-class>
 </servlet>
 
 <servlet-mapping>
<servlet-name>Helloworld</servlet-name>
<url-pattern>/aaa</url-pattern> 
 </servlet-mapping>

~~~~~~~~~~~~~~~~~

访问http://localhost:8080/test/aaa

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页