JSP中的参数传递方法
(1)利用<jsp:param name="paramName" value="paramValue"></jsp:param>其中name为与属性相关联的关键词,value为属性的值。
示例:传常量字符串<param name="userName" value="shilei"/>
示例:传变量 User user=new User("shilei");//User是一个类
<jsp:param name="user" value="<%=user%>"/>//<%=user%>里面括起来的是jsp表达式
(2)利用session.setAttribute("attributeName",attribute)或者request.setAttribute("attributeName",attribute)
示例:User user=new User("shilei");
session.setAttribute("userName",user);
(3)就像传统的html那样利用表单专递参数。
示例:<form name="loginForm" action="checkLogin.jsp" method="get">
<input type="text" name="username">
<input type="password" name="userpwd">
<input type="submit" value="提交">
</form>
(4)直接挂在URL后面。示例:response.sendRedirect("checkLogin.jsp?username="+username+"&userpwd="+userpwd"");
String name=request.getParameter("name");
<a href="welcome.jsp?name=<%=name%>">
(5)利用cookie对象来传递
Cookie cookie=new Cookie("my","liuliu")
cookie.setMaxage(60*60);(以秒为单位)最大的生命周期
response.addCookie(cookie);
Cookie[] cookies=request.getCookies();(可以通过遍历此数组来访问值)
相关文档:
以前用WSAD wizard做的,都可以在JSP页面中解析到EL表达式,当然前提是JSP2.0的情况下。
今天遇到了一个莫名其妙的问题。刚下载Eclipse3.3+MyEclipse6.0体验的过程中,遇上了解析不到EL表达式的问题。经过好几个小时的琢磨终于发现了,给大家share一下:
问题就出在建Web Project的时候web.xml声明上。
web.xml ......
在不允许目录浏览的情况下 浏览器会先找到首页(默认为index.html /index.jsp...)
否则出错
因此 若首页被恶意更改 网站崩溃
解决方法:
更改默认启动页面:$tomcat/conf/web.xml最后位置
<welcome-file-list>
<welcome-file>index.html</welcome-file> ......
jsp的pageEncoding="UTF-8",struts的encoding="UTF-8",eclipse 的工作区间的编码方式是GBK,但是在struts的action中获得的jsp传来的中文中有乱码,使用new String(sysgkForm.getContent().getBytes("UTF-8"),"GBK")转码也不行,最后发现问题出在了jsp里面。加上contentType="text/html; charset=GBK" 就好了。
pageEncodi ......
·pageContext:提供对页面属性的访问。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
pageContext.setAttribute("basePath",basePath);
%>
使用:
${pageScope}
·reques ......
1、如何混合使用Jsp和SSI #include?
在JSP中可以使用如下方式包含纯HTML:
但是如果data.inc中包含JSP CODE ,我们可以使用:
2、如何执行一个线程安全的JSP?
只需增加如下指令
3、JSP如何处理HTML FORM中的数据?
通过内置的request对象即可,如下:
String item = request.getParameter("item");
int howM ......