常用的web服务器有:
1、IIS,出自微软
2、Apache,linux,C语言写的
3、tomcat是Apache项目中的一个,java写的
servlet是服务器上运行的程序其主要功能在于交互式地浏览和修改数据,生成动态Web内容。
先记录下功能代码吧,关于servlet的原理,等之后学习更深入了,再补充一下。
- 生命周期
public class TestLiftCycleServlet extends HttpServlet {
/**
* 实例化
*/
public TestLiftCycleServlet() {
System.out.println("constructor");
}
/**
* 处理请求 多次使用
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
}
/**
* 初始化 只执行一次
*/
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("init");
}
/**
* 退出服务时调用
*/
@Override
public void destroy() {
System.out.println("destroy");
}
}
- 显示一个点击跳转的功能
package com.hq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorld() {
super();
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
super.service(arg0, arg1);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("<a href='http://www.baidu.com'>go</a>");
PrintWriter out = response.getWriter();
out.print(" 这个一个百度跳转 ");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);//和get使用同一套处理方法
}
}
- 获取onSubmit指定参数值
package com.hq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ThreeParams extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.println("获取到的参数 <br/>");
pw.println(req.getParameter("param1"));
pw.println("<br/>");
pw.println(req.getParameter("param2"));
pw.println("<br/>");
pw.println(req.getParameter("param3"));
pw.println("<br/>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post");
doGet(req, resp);
}
}
- 获取全部提交参数
package com.hq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReadAllParamsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=gb2312");
PrintWriter pw = resp.getWriter();
pw.println("获取到的所有参数 <br/>");
Enumeration paramsName = req.getParameterNames();
while(paramsName.hasMoreElements()){
String paramName = (String)paramsName.nextElement();
String[] paramValues = req.getParameterValues(paramName);
if(paramValues.length==1){
String paramValue = paramValues[0];
if (paramValue.length()==0) {
pw.println("no value");
}else {
pw.print(paramValue);
}
}else{
for (int i = 0; i < paramValues.length; i++) {
pw.println(paramValues[i]);
}
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post");
doGet(req, resp);
}
}
- 获取cookie值
package com.hq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//模拟设置cookie
for(int i = 0; i<3;i++){
Cookie cookie = new Cookie("Session-Cookie-"+i,"Cookie-Value_S"+i);
resp.addCookie(cookie);
cookie= new Cookie("Persistent-Cookie-"+i, " hello");
cookie.setMaxAge(3600);//时效性
resp.addCookie(cookie);
}
//设置网页的字符编码格式为gb2312
resp.setContentType("text/html;charset=gb2312");
PrintWriter pw = resp.getWriter();
//2 获取cookie
pw.println("获取到cookie");
Cookie[] cookies = req.getCookies();
if(cookies!=null){
Cookie cookie = null;
for(int i = 0; i < cookies.length; i++){
cookie= cookies[i];
pw.println("name:"+cookie.getName()+" value:"+cookie.getValue());
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post");
doGet(req, resp);
}
}
- 关于Session
/**
*
* session是存在服务器端的,cookie是存在客户端的。
* session可与浏览器关联,每个窗口有独一无二的sessionId.session就是服务端的一个内存,能存储任何内容。键值对
* session是针对窗口的。不像cookie拥有路径访问问题,同一个application下的servlet/jsp可以共享一个session。需要同一个客户端窗口
*
* 如果浏览器支持cookie,创建session时会把sessionId保存在cookie里,保持不变。否则sessionId每次都说新生成的
*
* session实现: cookie实现、url重写实现
*
*
* cookie(临时的,写在文件里)实现:sessionId存在临时cookie里
* 使用url重写的方式实现session,response。encodeURL(),转码,url后面添加sessionId
*
*http://blog.csdn.net/robbyo/article/details/17733743
*/
public class SessionDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取当前的session,若无,就创建一个session
HttpSession mySession = req.getSession(true);
resp.setContentType("text/html");
PrintWriter out= resp.getWriter();
String heading;
Integer accessCount = (Integer)mySession.getAttribute("accessCount");
if(accessCount==null){
accessCount = new Integer(0);
heading="welcome,new comer";
}else{
heading="welcome back";
accessCount = new Integer(accessCount.intValue()+1);
}
mySession.setAttribute("accessCount", accessCount);
out.println("<H3>show info saved in session</H3>"+accessCount);
out.println("<H3>create Information</H3>");
out.println(" new Session:"+mySession.isNew());
out.println("session id:"+mySession.getId()+
",create time:"+mySession.getCreationTime()
//最近一次访问的时间,session是有过期时间的
+",last access time:"+mySession.getLastAccessedTime());
out.println("<H3>Request Information</H3>");
out.println("Session ID from Request: "
//上次使用的session
+ req.getRequestedSessionId());
out.println("<BR>Session ID via Cookie: " //方式1:通过cookie
+ req.isRequestedSessionIdFromCookie());
out.println("<BR>Session ID via rewritten URL: " //方式2:通过重写url
+ req.isRequestedSessionIdFromURL());
out.println("<BR>Valid Session ID: " //session是否有效
+ req.isRequestedSessionIdValid());
//添加了一个刷新,浏览器不使用cookie,就在url里面添加session
out.println("<a href="+"SessionDemo"+">simple url </a>");
//经测试,360不行,chrome可以.response.encodeURL的功能是url后添加session。
String urlWithSession = resp.encodeURL("SessionDemo");
out.println("<a href='"+urlWithSession+"'>url with session</a>");
out.close(); // close output stream
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post");
doGet(req, resp);
}
}
- application
/**
* application里面的值是共享的
* @author dell
*
*/
public class TestServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=gb2312");
PrintWriter out = resp.getWriter();
//获取application
ServletContext application = this.getServletContext();
//设置application里的内容
Integer accessCount = (Integer)application.getAttribute("accessCount");
if(accessCount==null){
accessCount = new Integer(0);
}else{
accessCount = new Integer(accessCount.intValue()+1);
}
application.setAttribute("accessCount", accessCount);
//获取application里的内容
out.println("get count:"+accessCount);
}
}