jsp/servlet基础知识

1、基本概念

1.1、什么是jsp

在html页面中嵌入Java脚本代码

<%@ page language="java" import="java.util.*,java.text.*" 
	contentType= "text/html; charset=utf-8" %>
<html>
    <head>
        <title>输出当前日期</title>
    </head>
    <body>
        你好,今天是
        <% SimpleDateFormat formater =
              new SimpleDateFormat("yyyy年 MM月dd日");
    	    String strCurrentTime = formater.format(new Date()); %>	
        <%=strCurrentTime %>
    </body>
</html>

jsp的page属性

在这里插入图片描述

在JSP页面中计算两个数的和,将结果输出显示

在这里插入图片描述

总结

在这里插入图片描述

2.1、get、post

概念

GET和POST是HTTP请求的两种基本方法,一般用在表单提交中

<form action="welcome.jsp" method="GET">
		登录名:<input type="text" id="loginName" name="loginName"><br>
		密    码:<input type="text" id="password" name="password"><br>
		<input type="submit" id="submit">
	</form>
<form action="welcome.jsp" method="POST">
		登录名:<input type="text" id="loginName" name="loginName"><br>
		密    码:<input type="text" id="password" name="password"><br>
		<input type="submit" id="submit">
	</form>

get和post的区别

在这里插入图片描述在这里插入图片描述

doGet/doPost

2、内置对象

2.1、九大内置对象

在这里插入图片描述

2.2、解决页面中出现的中文乱码

idea默认utf-8,但是eclipse编码需要改变编码方式

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
<%@ page language="java"contentType="text/html";charset=utf-8"%>
//注意统一编码
request.setCharacterEncoding("utf-8");
//响应编码设置
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("utf-8");

如果get请求出现乱码

//治标的方法:
new String(s.getBytes(“iso-8859-1”),”utf-8”);
//治本的办法:配置tomcat\conf\server.xml文件
//修改为URIEncoding=”utf-8”,useBodyEncodingForURI=”ture”

2.3、在请求中保存和获取属性

在这里插入图片描述

2.5、request

request对象常用方法

request对象主要用于处理客户端请求
在这里插入图片描述

<form name="form1" method="post" action="reginfo.jsp">
    <table border="0" align="center">
      <tr> <td>用户名</td><td> <input type="text" name="name"></td></tr>
      <tr><td>密码</td><td > <input type="password" name="pwd"> </td></tr>
      <tr> <td>信息来源</td><td> 
          <input type="checkbox" name="channel" value="报刊">报刊 
          <input type="checkbox" name="channel" value="网络">网络 
          <input type="checkbox" name="channel" value="朋友推荐"> 朋友推荐 
          <input type="checkbox" name="channel" value="电视"> 电视
        </td></tr>
       <!--省略提交、取消按钮 -->
    </table>
</form>
<%
    //读取用户名和密码
    String name = request.getParameter("name");
    String pwd = request.getParameter("pwd");
    ……//HTML页面
    //读取复选框选择项
    String[] channels = request.getParameterValues("channel");
    if (channels != null) {
for (String channel : channels) {
out.println(channel);
}
    }
%>

2.6、response

response对象用于响应客户请求并向客户端输出信息
在这里插入图片描述
页面重定向
void sendRedirect(String location)
客户端将重新发送请求到指定的URL

以下代码实现登陆验证并跳转到欢迎页面

<%
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if ("sa".equals(name) && "sa".equals(pwd))       
	response.sendRedirect("welcome.jsp");
%>

但是页面跳转,请求的信息不一定跟着一起跳转,此时可以使用转发代替重定向实现页面跳转
转发的作用:在服务器端,将请求发送给服务器上的其他资源,以共同完成一次请求的处理
RequestDispatcher对象的forward()方法实现转发

<%
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
%>

在这里插入图片描述在这里插入图片描述在这里插入图片描述

2.7、session

一个会话就是在浏览器和服务器之间的一次通话,如果接通就会一直在会话,直到挂断
会话可以在多次请求中保存和使用数据
在这里插入图片描述
获取用户请求的登录信息进行验证

<%
if ("admin".equals(name) && "admin".equals(pwd)) {  // 如果是已注册用户
session.setAttribute("login", name);
 // 设置session过期时间
session.setMaxInactiveInterval(10*60);
request.getRequestDispatcher("admin.jsp").forward(request, response);
} else {
response.sendRedirect("index.jsp");
} 
%>

每个session都有唯一的id,这个id可以直接打印出来

在这里插入图片描述
在这里插入图片描述

Session.invalidate();//让session失效,手动设置无效
Session.removeAttribute(“userName”);//移除session中指定的数据
Session.setMaxInactiveInterval(5);//设置session的生命周期

然后通过设置项目的web.xml或Tomcat目录下的/conf/web.xml文件,单位是分钟

<session-config>
    <session-timeout>10</session-timeout>
</session-config>

整个session的过程如下
在这里插入图片描述

如何避免多余代码

可以将一些共性的内容写入一个单独的文件中,然后通过include指令引用该文件
创建登录验证文件 loginControl.jsp

<%
String login = (String) session.getAttribute("login");
if (login == null) {
  response.sendRedirect("index.jsp");
	return;
} %>

在后台首页面中使用include指令引用登录验证文件

<%@  include file="loginControl.jsp" %>

2.7、cookie

使用cookie自动填充用户名
在这里插入图片描述在这里插入图片描述
步骤
1、将用户名保存到cookie
Cookie cookie=new Cookie(“userName”,usernName);
2、
Response.addCookie(cookie);

2.8、application

在这里插入图片描述

<%\
Int count=1;
Object count1=application.getAttribute(“count”);
If(count1!=null){
Count=count+Integer.ParseInt(count1.toString());
}
%>
<h3>
<%=count%>
</h3>

在这里插入图片描述在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章