servlet使用
程序员文章站
2022-03-18 20:39:22
1、编写一个类来继承HttpServlet2、在新写的类中覆盖doGet()与doPost()方法,用来以自己想要的方式处理请求import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;public class Servlet1 extends HttpServ...
1、编写一个类来继承HttpServlet
2、在新写的类中覆盖doGet()与doPost()方法,用来以自己想要的方式处理请求
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class Servlet1 extends HttpServlet{
protected void doGet(HttpServletRequest rep, HttpServletResponse resp) throws IOException {
System.out.print("doGet方法");//后台代码
PrintWriter writer = resp.getWriter();
writer.print("Hello,Servlet");//向前台输出效果
}
protected void doPost(HttpServletRequest rep, HttpServletResponse resp) throws IOException{}
}
3、注册servlet,在web.xml中写映射,一个servlet对应一个servlet-mapping ,名字(servlet-name)是用来辨识一对一关系的,一个servlet可以被多个servlet-name映射
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>halloservlet</servlet-name>
<servlet-class>Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>halloservlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
本文地址:https://blog.csdn.net/kitahiragawa/article/details/112008785
上一篇: ai怎么绘制跳一跳效果的方块猪头效果?
下一篇: 用路由器限制别人的网速的技巧