WebServlet使用方法
程序员文章站
2022-04-23 07:52:24
...
在servlet3.0以后,我们可以不用再web.xml里面配置servlet,只需要加上@WebServlet注解就可以修改该servlet的属性了。
**
1.Form表单连WebServlet**
Servlet部分:
package WebTest1;
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 WebTest1
*/
//对类进行标记,注解。框架搭建者并不知道使用框架的人所建立的类,所以要通过反射来获取用户定义的类。
@WebServlet("/WebTest1")
public class WebTest1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WebTest1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
//处理Get请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决中文乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
//接收参数
String str = request.getParameter("mytext");
//返回数据
response.getWriter().print(str+"你好");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
//处理Post请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Html5部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="./WebTest1" method="get">
要发送的数据:<input type="text" name="mytext" />
<input type="submit" value="提交" />
</form>
</body>
</html>
运行并输入:彭健
结果:
网页访问:localhost:端口号/文件夹名/html文件名.html
结果:
2.通过ajax连WebServlet
WebServlet部分:(同上面一样)
package WebTest1;
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 WebTest1
*/
//对类进行标记,注解。框架搭建者并不知道使用框架的人所建立的类,所以要通过反射来获取用户定义的类。
@WebServlet("/WebTest1")
public class WebTest1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public WebTest1() {
super();
// TODO Auto-generated constructor stub
}
//处理Get请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决中文乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
//接收参数
String str = request.getParameter("mytext");
//返回数据
response.getWriter().print(str+"你好");
}
//处理Post请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
HTML5部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
数据:<input type="text" id="mytext">
<button onclick="send()">发送</button>
</body>
<script>
// $可以取元素、类、ID
function send(){
var data = $("#mytext").val();
$.ajax({
//访问servlet所在路径
url:"./WebTest1",
//data:{"key",value},
data:"mytext="+data,
//数据类型
//dataType:"json",
//默认ture 开启异步
async:true,
// 请求方式
type:'GET',
//后台返回内容
success:function(str){
alert(str);
},
error:function(e){
alert("error!");
}
})
}
</script>
</html>
运行结果:
下一篇: STL使用方法