JSP内置对象

来源:互联网 发布:淘客营销软件 编辑:程序博客网 时间:2024/05/18 00:22

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

在页面中获取参数




<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="utf-8"%>

<%
String id=request.getParameter("id"); //获取id参数的值
String user=request.getParameter("user");//获取user参数的值
String pwd=request.getParameter("pwd");//获取pwd参数值
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>处理页</title>
</head>
<body>
id参数的值为:<%=id %><br>
user参数的值为:<%=user %><br>
pwd参数的值为:<%=pwd %>
</body>

</html>








<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>使用request对象获取请求参数值</title>
</head>
<body>
<a href="deal.jsp?id=1&user=">处理页</a>
</body>
</html>











///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>结果页</title>
</head>
<body>
<%String message=request.getAttribute("result").toString(); %>
<%=message %>
</body>
</html>













<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
try{//捕获异常信息
int money=100;
int number=0;
request.setAttribute("result",money/number);//保存执行结果
}catch(Exception e){
request.setAttribute("result","很抱歉,页面产生错误!");//保存错误提示信息
}
%>
<jsp:forward page="deal.jsp"/>
</body>
</html>













/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>写入cookie</title>
<script type="text/javascript">window.location.href="index.jsp"</script>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");


String user=URLEncoder.encode(request.getParameter("user"),"utf-8");//获取用户名
Cookie cookie = new Cookie("mrCookie", user+"#"+new java.util.Date().toLocaleString());
cookie.setMaxAge(60*60*24*30);//设置cookie有效期30天
response.addCookie(cookie);//保存cookie
%>


</body>
</html>












<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>通过cookie保存并读取用户登录信息</title>
</head>
<body>
<%
Cookie[ ]   cookies = request.getCookies();//从request中获得Cookie对象的集合
String user = "";//登录用户
String date = "";//注册的时间
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {//遍历cookie对象的集合
if (cookies[i].getName().equals("mrCookie")) {//如果cookie对象的名称为mrCookie
user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);//获取用户名
date = cookies[i].getValue().split("#")[1];//获取注册时间
}
}
}
if ("".equals(user) && "".equals(date)) {//如果没有注册
%>
游客您好,欢迎您初次光临!
<form action="deal.jsp" method="post">
请输入姓名:<input name="user" type="text" value="">
<input type="submit" value="确定">
</form>
<%
} else {//已经注册
%>
欢迎[<b><%=user %></b>]再次光临<br>
您注册的时间是:<%=date %>
<%
}
%>
</body>
</html>









//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="show.jsp?name=张三&sex=男">解决中文乱码</a>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>通过cookie保存并读取用户登录信息</title>
</head>
<body>
<%
Cookie[ ]   cookies = request.getCookies();//从request中获得Cookie对象的集合
String user = "";//登录用户
String date = "";//注册的时间
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {//遍历cookie对象的集合
if (cookies[i].getName().equals("mrCookie")) {//如果cookie对象的名称为mrCookie
user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);//获取用户名
date = cookies[i].getValue().split("#")[1];//获取注册时间
}
}
}
if ("".equals(user) && "".equals(date)) {//如果没有注册
%>
游客您好,欢迎您初次光临!
<form action="deal.jsp" method="post">
请输入姓名:<input name="user" type="text" value="">
<input type="submit" value="确定">
</form>
<%
} else {//已经注册
%>
欢迎[<b><%=user %></b>]再次光临<br>
您注册的时间是:<%=date %>
<%
}
%>
</body>
</html>









//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="show.jsp?name=张三&sex=男">解决中文乱码</a>
</body>
</html>
















<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
name参数的值为:<%=new String(request.getParameter("name").getBytes("iso-8859-1"),"UTF-8") %><br>
sex参数的值为:<%=request.getParameter("sex") %>

</body>
</html>













//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


获取客户端信息




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用request对象的相关方法获取客户端信息</title>
</head>
<body>
<br>客户提交信息的方式:<%=request.getMethod()%>
<br>使用的协议:<%=request.getProtocol()%>
<br>获取发出请求字符串的客户端地址:<%=request.getRequestURI()%>
<br>获取发出请求字符串的客户端地址:<%=request.getRequestURL()%>
<br>获取提交数据的客户端IP地址:<%=request.getRemoteAddr()%>
<br>获取服务器端口号:<%=request.getServerPort()%>
<br>获取服务器的名称:<%=request.getServerName()%>
<br>获取客户端的主机名:<%=request.getRemoteHost()%>
<br>获取客户端所请求的脚本文件的文件路径:<%=request.getServletPath()%>
<br>获得Http协议定义的文件头信息Host的值:<%=request.getHeader("host")%>
<br>获得Http协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent")%>
<br>获得Http协议定义的文件头信息accept-language的值:<%=request.getHeader("accept-language")%>
<br>获得请求文件的绝对路径:<%=request.getRealPath("index.jsp")%>
</body>
</html>










//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>首页</title>
</head>
<body>
<%response.sendRedirect("login.jsp"); %>
</body>
</html>








<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录页面</title>
</head>
<body>
<form name="form1" method="post" action="">
用户名: <input name="name" type="text" id="name" style="width: 120px"><br>
密&nbsp;&nbsp;码: <input name="pwd" type="password" id="pwd" style="width: 120px"> <br>
<br>
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>










//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

在index.jsp页面中,提供用户输入用户名文本框,在session.jsp页面中。将用户输入的用户名保存在session对象中,用户在该页面可以添加最喜欢去的地方,在result.jsp页面中,显示用户输入的用户名与最想去的地方





<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/style.css">


  </head>
  
  <body>
  <form id="form1" name="form1" method="post" action="session.jsp">
    <div align="center">
  <table width="23%" border="0">
    <tr>
      <td width="36%"><div align="center">您的名字是:</div></td>
      <td width="64%">
        <label>
        <div align="center">
          <input type="text" name="name" />
        </div>
        </label>
        </td>
    </tr>
    <tr>
      <td colspan="2">
        <label>
          <div align="center">
            <input type="submit" name="Submit" value="提交" />
          </div>
        </label>
           </td>
    </tr>
  </table>
</div>
</form>
  </body>
</html>










<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'result.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">


  </head>
  
  <body>
    <div align="center">
  <%
 
  String name = (String)session.getAttribute("name");//获取保存在session范围内的对象
 
  String solution = request.getParameter("address");//获取用户输入的最想去的地方
   %>
<form id="form1" name="form1" method="post" action="">
  <table width="28%" border="0">
    <tr>
      <td colspan="2"><div align="center"><strong>显示答案</strong></div>          </td>
    </tr>
    <tr>
      <td width="49%"><div align="left">您的名字是:</div></td>
      <td width="51%"><label>
        <div align="left"><%=name%></div> <!-- 将用户输入的用户名在页面中显示 -->
      </label></td>
    </tr>
    <tr>
      <td><label>
        <div align="left">您最喜欢去的地方是:</div>
      </label></td>
      <td><div align="left"><%=solution%></div></td> <!-- 将用户输入的最想去的地方在页面中显示 -->
    </tr>
  </table>
</form>
  <p>&nbsp;</p>
</div>
  </body>

</html>













<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'session.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">


  </head>
  
  <body>
  <%
  String name = request.getParameter("name");//获取用户填写的用户名
 
  session.setAttribute("name",name);//将用户名保存在session对象中
   %>
    <div align="center">
  <form id="form1" name="form1" method="post" action="result.jsp">
    <table width="28%" border="0">
      <tr>
        <td>您的名字是:</td>
        <td><%=name%></td>
      </tr>
      <tr>
        <td>您最喜欢去的地方是:</td>
        <td><label>
          <input type="text" name="address" />
        </label></td>
      </tr>
      <tr>
        <td colspan="2"><label>
          <div align="center">
            <input type="submit" name="Submit" value="提交" />
            </div>
        </label></td>
      </tr>
    </table>
  </form>
  <p>&nbsp;</p>
</div>
  </body>
</html>















/////////////////////////////////////////////////////////////////////////////////////////////////////////////


在页面中显示page对象各方法的返回值







<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>page对象各方法的应用</title>
</head>
<body>
<%! Object object; //声明一个Object型的变量 %>
<ul>
<li>getClass()方法的返回值:<%=page.getClass()%></li>
<li>hashCode()方法的返回值:<%=page.hashCode()%></li>
<li>toString()方法的返回值:<%=page.toString()%></li>
<li>与Object对象比较的返回值:<%=page.equals(object)%></li>
<li>与this对象比较的返回值:<%=page.equals(this)%></li>
</ul>
</body>
</html>

















//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误提示页</title>
</head>
<body>
错误提示为:<%=exception.getMessage() %>
</body>
</html>




















<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8" errorPage="error.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用exception对象获取异常信息</title>
</head>
<body>
<%
request.setAttribute("price","12.5元");//保存单价到request范围内的变量price中
float price=Float.parseFloat(request.getAttribute("price").toString());//获取单价,并转换为float型
%>
</body>
</html>















//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


编写一个jsp程序,实现用户登入,当输入的用户名和密码错误时,将页面重新定向到错误提示页面,并在该页面显示30秒后,自动返回到用户登入页面








<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%
request.setCharacterEncoding("GB18030");
String username=request.getParameter("username");
String pwd=request.getParameter("pwd");
if("mr".equals(username) && "mrsoft".equals(pwd)){
out.print("<script language='javascript'>alert('登录成功!');window.location.href='index.jsp';</script>");
}else{
response.sendRedirect("error.jsp");
}
%>
















<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%
response.setHeader("refresh","30;URL=index.jsp");
%>
    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>错误提示页</title>
</head>
<body>
您输入的用户名或密码错误,请重新登录!
</body>
</html>
















<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录</title>
</head>
<body>
<form name="form1" method="post" action="deal.jsp">
  用&nbsp;户&nbsp;名:  <input name="username" type="text" id="username">  *<br>
密&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;<input name="pwd" type="password" id="pwd">*<br>
<input type="submit" name="Submit" value="登录">&nbsp;
<input name="Reset" type="reset" id="Reset" value="重置">
</form>


</body>
</html>













//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


编写一个简单的留言簿,实现添加留言和显示留言内容等功能




<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%
request.setCharacterEncoding("GB18030");
String author=request.getParameter("author"); //获取留言人
String content=request.getParameter("content"); //获取留言内容
String message="["+author+"]说:"+content+"<br>"; //组合留言信息
if(session.getAttribute("message")!=null){
message+=session.getAttribute("message").toString();
}
session.setAttribute("message",message); //将留言信息保存到session中
response.sendRedirect("index.jsp");
%>












<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>签写留言信息</title>
<script language="javascript">
function checkNull(form){
for(i=0;i<form.length;i++){  
if(form.elements[i].value == ""){         //form的属性elements的首字e要小写
alert("很抱歉,"+form.elements[i].title + "不能为空!");
form.elements[i].focus();//当前元素获取焦点
return false;
}
}
}
</script>
</head>
<body>
<%
if(session.getAttribute("message")!=null){
out.println(session.getAttribute("message").toString());
}
%>
<br><hr></hr>
<form name="form1" method="post" action="deal.jsp" onSubmit="return checkNull(form1)">
留言人:&nbsp;&nbsp;
<input name="author" type="text" id="author" size="30" title="留言人"><br>
留言内容:
<textarea name="content" cols="70" rows="10" title="留言内容" id="content"></textarea><br>
<input name="Submit" type="submit" value="签写留言">
<input name="Submit2" type="reset" value="重置">


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

















//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////















1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 着火了怎么办 我的世界 生存战争2感冒了怎么办 生存战争2吐了怎么办 我的世界hqm重置怎么办 不小心打了110怎么办 我的世界皮肤有黑影怎么办 我的世界字体变大了怎么办 生锈的铁钉扎了怎么办 每天晚上窗纱上老有蝙蝠倒挂怎么办 我的世界没有痒怎么办 七日杀被ban了怎么办 吕框箱子上保护摸撕不掉怎么办 我的世界开光影卡怎么办 我的世界买不了怎么办 我的世界延迟高怎么办 我的世界过于昂贵怎么办 白色麻布染上别的颜色怎么办 印度老山檀香开裂了怎么办 专升本没过线怎么办 西安公租房小孩上学怎么办 全民k歌直播没人怎么办 在全民直播没人看怎么办 皮肤又黄又粗怎么办 被强制消费后应怎么办? 当保安不发工资怎么办? 辅警改革流管员怎么办 退伍证上照片毁了怎么办 辅警年龄大了怎么办 交警2小时不出警怎么办 中暑发烧39度了怎么办 中暑头疼怎么办最快最有效 十五个月的宝宝拉肚子怎么办 中暑了头疼想吐怎么办 2周岁中暑了呕吐怎么办 容易中暑的人该怎么办 喷泡3m反光脏了怎么办 新摩托车被交警查到怎么办 写字楼保安夜班巡逻害怕怎么办 全民k歌歌曲删了怎么办 莲藕洞里的黑膜怎么办 鞋子后面的拉链磨脚怎么办