1. JSP调用javaBean语法:
<jsp:useBean id="cb" class="bean.CountBean" scope="request" type="">
<!--<% CountBean cb = new CountBean()%>-->
</jsp:useBean>
<jsp:setProperty name="cb" property="count" value=”23”{param="count"}/>
<!--<% cb.setCount(Integer.paseInt("23"))%>-->
<jsp:getProperty name="cb" property="count"/>
<!--<% out.print(cb.getCount())%>-->
parma="count" <--> value="<%=request.getParamter("count")%>"
2.路径问题
./ 开始是相对于当前路径
/ 是相对于根目录的绝对路径
../ 是退到它的上一级目录
<%
String path = request.getContextPath();//web应用上下文:/login_database
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //http://localhost:8089/login_database/
%>
http://topic.csdn.net/u/20091122/21/0809f147-23ec-4c7c-a563-1c0997ed5807.html?32491
先看一下下面的URL:
http://192.168.1.1:8080/hello/index.html
一个URL由
服务器地址: http://192.168.1.1:8080
WEB应用上下文: /hello
网页或请求: /index.html
三个部分组成。
WEB应用发布时,WEB应用上下文可以自行指定,如放在ROOT/下的话,WEB应用上下文就是空,如:
http://192.168.1.1:8080/index.html
它的WEB应用上下文就是空。
HTML协议中要求,form的action属性,以“/”开头是绝对路径,不以“/”开头的是相对路径。
绝对路径是相对于服务器地址而言的,相对路径是相对于当前网页或请求而言的。
举一个例子,你的当前网页位置是:
http://192.168.1.1:8080/hello/page/index.html
其中/hello是WEB应用上下文;/page/index.html是网页。
在这个网页上有一个请求需要指向
http://192.168.1.1:8080/hello/servlet/checkUser.cgi
index.html中的form应该怎么写呢?
可以写成绝对路径(相对于http:/192.168.1.1:8080)
<form action="/hello/servlet/checkUser.cgi" >
...
</form>
也可以写成相对路径(相对于http://192.168.1.1:8080/hello/page)
<form action="../servlet/checkUser.cgi" >
...
</form>
Servlet To JSP
//forward
<jsp:forward page="b.jsp" />
<jsp:param name="user" vale="他们"/>
</jsp:forward>
getServletConfig().getServletContext().getRequestDispatcher("/servlet_jsp/ServletUseJsp.jsp").forward(req, resp);
//sendredirect
<%response.sendRedirect();%>
resp.sendRedirect(req.getContextPath() + "/servlet_jsp/ServletUseJsp.jsp");
3.Session and Cookies
/**
* 用于演示Servlet API中的Session管理机制
*/
//get current session or, if necessary, create a new one
HttpSession mySession = request.getSession(true);
//MIME type to return is HTML
response.setContentType("text/html");
//get a handle to the output stream
PrintWriter out = response.getWriter();
//generate HTML document
.......................
Cookies
Cookie cookie = new Cookie("Session-Cookie-" + i, "Cookie-Value-S" + i);
response.addCookie(cookie);
cookie = new Cookie("Persistent-Cookie-" + i, "Cookie-Value-P" + i);
// Cookie is valid for an hour, regardless of whether user quits browser, reboots computer, or whatever.
cookie.setMaxAge(3600);
response.addCookie(cookie);
4.jsp四个作用域(信息共享范围):page,request,session,application
page :jsp 当前页面(默认)
request :一个HTTP请求,在当前页面-服务器-目的页面
session :窗口、子窗口发送不同的HTTP请求
application :不同的窗口,浏览器访问同一个应用服务
5.Tomcat下乱码解决方案
页面本身有中文:
Servlet resp.setContentType("text/html;charset=gbk");
JSP <%@page contentType="text/html;charset=gbk"%>
Get方式传值
修改server.xml-->URIEncoding="GBK"
Post方式传值
request.setCharacterEncoding("GBK")
<jsp:param name="user" value="<%=s%>"/>
<%request.setCharaterEncoding("GBK");%>
6.静态包含和动态包含
Dynamic include
<jsp:include page="b.jsp?user=啊啊啊啊啊">
Static include
<%@include file="b.jsp"%>