其他分享
首页 > 其他分享> > Servlet

Servlet

作者:互联网

Servlet执行过程

 

 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("你好呀!");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }
}

浏览器访问HelloServlet,HelloServlet.java是java文件,需要将java文件转换为浏览器认识的东东:

 <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>servlet.HelloServlet</servlet-class>
  </servlet>
 <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

  

标签:java,Servlet,resp,import,servlet,javax,HelloServlet
来源: https://www.cnblogs.com/jzbl/p/16404090.html