首页 > 编程语言 > 详细

【Javaweb】Servlet的xml和注解配置

时间:2019-11-01 14:48:29      阅读:110      评论:0      收藏:0      [点我收藏+]

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,利于调试

【Javaweb】Servlet的xml和注解配置

原文:https://www.cnblogs.com/SeasonBubble/p/11776856.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!