您的位置:首页 > Web前端 > JavaScript

jsp和servlet知识点整理

2017-12-27 09:59 441 查看

JSP

Page作用域指本JSP页面的范围

pageContext.setAttribure(key,value)
pageContext.getAttribute(key)

cookie的作用

cookie是web服务器存在客户端的一系列文本信息
1.对特定对象的追踪
2.统计网页浏览次数
3.简化登录
4.安全性能:信息容易泄露
 

在JSP中使用cookie

创建cookie对象

Cookie ck1 = new Cookie(String key,Object value);

写入cookie

response.addCookie(ck1);

读取cookid

Cookie[] cookies = request.getCookies();

one.jsp

<%
String name = request.getParameter(“uname”);
String pwd = request.getParameter(“upwd”);
Cookie ck1 = new Cookie(“uname”,name);
Cookie ck2 = new Cookie(“upwd”,pwd);
ck1.setMaxAge(60*60);
ck2.setMaxAge(60*60);
response.addCookie(ck1);
response.addCookie(ck2);
response.sendRedirect(“two.jsp”);
%>

two.jsp

<%
Cookies ck = request,getCookies();
String name = “”;
String pwd = “”;
if(ck!=null) {
for(int i=0; i<ck.length; i++) {
if(ck[i].getName().equals(“uname”)){
name = ck[i].getValue();
}else if(ck[i].getName().equals(“upwd”)){
pwd = ck[i].getValues();
}
}
}
%>

Servlet

PrintWriterout = response.getWritter();
out.write(“<scriptlanguage=’javascript’>”);
out.write(“alert(‘wrong’)”);
out.write(“window.location.href=’index.jsp’”);
out.write(“</script>”);

EL表达式(ExpressionLanguage)

功能:替代jsp页面中的复杂代码
可以去除实体类中的私有属性
对于list集合,可通过下标方式找相关元素
得到某个数据时可以自动转换类型
 
若web.xml是2.5版本的话,那么需要支持EL表达式:isElIgnored=“false” 若是3.0版本就不用添加EL表达式

LoginServlet.java中

request.setAttribute(“stu”,list);
request.getRquestDispatcher(“one.jsp”).forward(request,response);

one.jsp中

${stu[0].name}
${stu[1].age}

取出map集合数据

${map[“name”].name}
 

JSTL jsp标准标签库

<c:if test=”${not empty name}”>

${uname}
</c:if>
<c:iftest=”${empty name}”></c:if>

<c:choose>

<c:when test=”${not empty name}”>
${name}
</c:when>
<c:otherwise>
<jsp:forward page=”index.jsp”/>
</c:otherwise>
</c:choose>

<c:forEach items=”${listn}” var=”lisn”>

<li><${lisn.name}</li>
</c:forEach>

计数

<%
Integer count = (Integer)application.getAttribute(“count”);
if(count!=null){count++;}
else{count=1;}
application.setAttribute(“count”,count);
%>

隔行变色

<c:forEachitems=”${listn}” var=”lstn” varstatus=”status”>
<li <c:if test=”${status:index%2==1}”>style=”background-color:red;”</c:if>></li>
</c:forEach>
 

如果两个jsp之间通过a标记传递参数,需要用一个关键字Param(参数化)

${Param.传递值名}

隐藏域

<input type=”hidden” name=”hide” value=”${param.nid}”>

将数据库中时间改为年月日,在jsp页面中更改,使用formatDate

<%@ taglib uri=”../../fmt/” prefix=”fmt”%>
<fmt:formatDate value=”${lstn.fbtime}” pattern=”yyyy-mm-dd”>

children子节点

parentNode父节点

enctype=”multipart/form-data”  支持多任务格式数据  二进制

get:最多只能传递255个字节

后台接受json数据

InputStreamis = request.getInputStream();  获取页面传递过来的流(JSON)
若页面传到后台的是一个object,没有解析
xmlHttp.send(Json.Stringify(data))   发送
直接发送json格式获得的是一个object,需要第三方json格式文件进行转换
 
JSONObjectobj = JSONObject.fromObject(str);
Stringcode = (String)obj.get(“code”);

中文乱码

字节转换字符时出现的问题
InputStreamReader(is,”utf-8”);
出现乱码时,还可以
response.setContextType(“text/html”);

在js中遍历map集合时,不能用java的方式遍历,因为js中既没有set,也没有iterator,需要用两层循环方式遍历

for(var i=0; i<a.length; i++){
var c= a[i];
for(var k in c){
alert(k+”:”+c[k])
}
}

下拉框的值能获取,change方法

$(“#province”).change(function(){
var province = $(this).val();
var pro = {province:province}; //json格式
$.ajax({
url:”ajax”,
type=”post”,
data:JSON.stringify(pro),
error:function(){alert(111)},
success:function(message){
var mess=eval(“(“+message+”)”);
for(var i=0; i<mess.length: i++) {
$(“#city”).append(“<optino>”+mess[i]+”</option>”);
}
}
});
});
 

一个页面

用户名:<%=name%>
<%
String name = request.getParameter(“uname”);
%>
 
<%
Enumeration en = request.getAttributeNames();
//Object obj = en.nextElement();
//out.print(obj);   //在页面打印
while(en.hasNextElements()){
out.print(en.nextElement());
}
%>
 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet jsp