1.xml
<%@ page language="java" contentType="text/html;" %> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <form name="form1" action="MyServlet"> user:<input name="user" type="text" id="user" size="25" maxlength="30"><br> pass:<input name="password" type="password" id="password" size="25" maxlength="30"><br> b<input type="submit" name="Sumbmit" value="提交"><br> </form> </body> </html>
<servlet> <servlet-name>form1</servlet-name>//这里是自定义的 <servlet-class>Try.MyServlet</servlet-class>//包名+servlet类名 </servlet> <!-- servlet的虚拟路径的配置 --> <servlet-mapping> <servlet-name>form1</servlet-name>//要和上面匹配 <url-pattern>/MyServlet</url-pattern>//类名,注意“/” </servlet-mapping>
package Try; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class MyServlet */ //@WebServlet("/MyServlet") public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MyServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("Login success"); String username=request.getParameter("user"); String password=request.getParameter("password"); System.out.print("user:"+username+"\npassword"+password); } }
2.注解
@WebServlet("/MyServlet")
未正式发布一般使用web.xml,利于调试
原文:https://www.cnblogs.com/SeasonBubble/p/11776856.html