概述
注解方式实现Servlet
1.Servlet3.0
Servlet3.0新增了一些注解,用来简化的javaweb代码开发:
可以省略web.xml配置文件
支持异步处理(多线程技术)
支持可插性特性(书写的代码编译后生成的class文件可以直接部署到其他项目的,自动加载执行)
2.具体实现
package test;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/zj")
public class ZhuServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("你好呀");
}
}
3.severlet获取多个值
使用getParameterValues方法
<%--
Created by IntelliJ IDEA.
User: shenh
Date: 2020/6/6
Time: 14:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>多个值</title>
</head>
<body>
<form action="/web01/hobby" method="post">
用户名:<input type="text" name="username" value="jack" /> <br/>
密码:<input type="text" name="password" value="1234" /> <br/>
爱好:<input type="checkbox" name="hobby" value="smoke" checked="checked"/> 抽烟
<input type="checkbox" name="hobby" value="drink" checked="checked" /> 喝酒
<input type="checkbox" name="hobby" value="head" /> 烫头 <br/>
<input type="submit" value="post提交" />
</form>
</body>
</html>
package test;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/hobby")
public class MyHobby extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println(username);
String password = request.getParameter("password");
System.out.println(password);
String[] hobbies = request.getParameterValues("hobby");
for (String temp:hobbies) {
System.out.println(temp);
}
}
}
getParameterMap方法可以获取所有参数
最后
以上就是动听手套为你收集整理的注解方式实现Servlet的全部内容,希望文章能够帮你解决注解方式实现Servlet所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复