欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java Servlet之HttpServlet

程序员文章站 2024-02-28 12:06:52
...

1、web.xml文件(放在WEB-INF文件下)指定访问类的路径

  • servlet和servlet-mapping通过servlet-name连接在一起
  • url-parttern为在浏览器*问的路径,前面记得要加/
    java Servlet之HttpServlet
 <servlet>
		<servlet-name>EServlet</servlet-name>
		<servlet-class>cn.itcast.web.servlet.EServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>EServlet</servlet-name>
		<url-pattern>/EServlet</url-pattern>
</servlet-mapping>

2、EServlet类(例子)

package cn.itcast.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EServlet extends HttpServlet {
//HttpServlet虽然是抽象类 但没有任何抽象方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
		System.out.println("dopost()...");
	}
}

3、html文件(WebRoot文件夹下)

<!DOCTYPE html>
<html>
  <head>
    <title>login.html</title>	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  </head> 
  <body>
    This is my HTML page. <br>
    <form action="/day09_1/EServlet" method="post">//这里方法定义为post,动作指向EServlet类
     <input type="submit" value="点击提交"/>
    </form>
  </body>
</html>

4、结果:EServlet类没有重写doGet方法 所以显示405错误
(只重写了doPost方法,只支持post方法)
java Servlet之HttpServlet