您的位置:首页 > 编程语言 > Java开发

JavaWeb Servlet 入门使用

2017-05-10 09:57 381 查看

Servlet 简介

Servlet(Server Applet),全称JavaServlet,未有中文译文。是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。

Servlet运行于支持Java的应用服务器中。从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器

session和cookie区别

存储位置不同。session存储在服务器,cookie存储在浏览器。

cookie会长时间保存,因为它是在本地保存。session不会长时间保存,因为它是在服务器保存的,当会话结束,session也就被销毁了。

session和cookie之间的关联

当客户端请求服务器创建一个session(会话)时,服务器会首先检查客户请求头部是包含session标识(session_id),因为只要客户端与服务器产生过session,那么服务器就会在本地创建一个cookie,其中就包括sessionid,如果以上条件都不满足,服务器就会创建一个会话,并随机分配一个sessionid给客户端

Servlet 演示Demo

任务目标

jsp页面访问java类,形成session(会话)

java类 获取和设置session

在浏览器cookie被禁用的情况下,服务器是如何保证session(会话)不丢失的。

程序运行顺序

Index.jsp(请求)——>LoginServlet.java(验证通过跳转)——>IndexServlet.java(程序运行结束)

Index.jsp 登陆页面

<html>
<head>
<title>登录页面</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">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="userName"/>
<br/>
密码:<input type="text" name="userPwd"/>
<br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

</form>
</body>
</html>


Fail.jsp 登陆错误页面

<html>
<head>
<title>信息提示页面</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">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
<font color='red' size='3'>亲, 你的用户名或密码输入有误!请重新输入!</font><br/>
<a href="index.jsp">返回登录页面</a>
</body>
</html>


LoginServlet.java

package demo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// 扩展 HttpServlet 类
public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.接收参数
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");

//2.判断逻辑
if("123456".equals(userName)
&& "123456".equals(userPwd)){
//登录成功
/**
* 分析:
*    context域对象:不合适,可能会覆盖数据。
*    request域对象: 不合适,整个网站必须得使用转发技术来跳转页面
*    session域对象:合适。
*/

/**
* 一、登录成功后,把用户数据保存session对象中
*/
//1.创建session对象
HttpSession session = request.getSession();

//2.获取随机服务器分配给我的session_id
String id = session.getId();
System.out.println("当前的session_id:"+id);

//3.把数据保存到session域中
session.setAttribute("loginName", userName);

//4.动态改写URL 也就是url后加追加上当前会话的 session_id
String encodeRedirectURL = response.encodeRedirectURL(request.getContextPath()+"/IndexServlet");
//跳转到用户主页
response.sendRedirect(encodeRedirectURL);

}else{
//登录失败
//请求重定向
response.sendRedirect(request.getContextPath()+"/fail.jsp");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}


IndexServlet.java

package demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class IndexServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();

String html = "";

/**
* 接收request域对象的数据
*/
/*
String loginName = (String)request.getAttribute("loginName");
*/

/**
* 二、在用户主页,判断session不为空且存在指定的属性才视为登录成功!才能访问资源。
* 从session域中获取会话数据
*/
//1.得到session对象
HttpSession session = request.getSession(false);
if(session==null){
//没有登录成功,跳转到登录页面
//              response.sendRedirect(request.getContextPath()+"/login.html");
//              return;
System.out.println("当前会话丢失");
}
//2.取出会话数据
String loginName = (String)session.getAttribute("loginName");
String id = session.getId();
if(loginName==null){
//没有登录成功,跳转到登录页面
//              response.sendRedirect(request.getContextPath()+"/login.html");
//              return;

c6ac
System.out.println("用户名信息丢失- 用户名");
}

html = "<html><body>欢迎回来,"+loginName+",<a href='"+request.getContextPath()+"/LoginServlet'>安全退出</a></body></html>";

System.out.println("Index页面的session_id:"+id);
writer.write(html);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息