servlet学习(一)
servlet 百科名片
Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。
它担当客户请求(Web浏览器或其他HTTP客户程序【request】)与服务器响应(HTTP服务器上的数据库或应用程序【response】)的中间层。
Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,
该Web服务器必须包含支持Servlet的Java虚拟机。
servlet 的命名
Server + Applet =Servlet 意为服务器端的小程序
servlet工作模式
1.客户端发送请求至服务器
servlet实例
目前servlet最高版本是servlet3.0(servlet新特性)这里以servlet2.5为例,
servlet类
package com.insigma.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecondServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public SecondServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String name = request.getParameter("user");
PrintWriter out = response.getWriter();
out.println(name);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应类型
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("user");
PrintWriter out = response.getWriter();
out.print("你好:"+name);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>secondHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript" >
$(document).ready(function(){
$("input[type='button']").click(function(){
$.ajax({
url:"servlet/SecondServlet",
type:"post",
data:"user="+$("input[type='text']").val(),
success:function(dta){
alert(dta);
}
});
});
});
</script>
</head>
<body>
<input type="text"><input type="button" value = "提交">
</body>
</html>
总结:
request.setCharacterEncoding("utf-8");//设置请求的字符类型
response.setContentType("text/html;charset=UTF-8");//响应类型应于请求类型相同
html中用到ajax其实也可用form表单
<form action="servlet/FirstServlet" method="post">
<input type="text" name = "username"><input type="submit" name = "submit" value = "提交">
</form>
注意:提交按钮 type = "submit”
在$.ajax中date中,必须加上键值(“user”)于servlet中request.getParameter("user")一致!
由于编写java代码时,用“;”结束,而ajax中以","结束,不然编译错误;
在ajax中,type类型不同,url也不同,上面用的post类型
如果用get类型代码如下
$.ajax({
url:"servlet/SecondServlet?username="+$("input[type = 'text']").val(),
type:"get",
success:function(data){
alert(data);
}
});
由于初来乍道,总结能力太差,希望读者能提出宝贵的意见!