jsp笔记
来源:互联网 发布:淘宝店家开通蚂蚁花呗 编辑:程序博客网 时间:2024/06/06 16:55
一、tomcat和Servlet的配置
1、eclipse集成tomcat的配置
2、在web.xml中配置和映射servlet
ServletConfig功能
1、 获取servlet标签中配置的<init-param>标签中的值
2、获取ServletContext然后获取<context-param>标签中的值
ServletContext功能
1、获取<context-param>标签中的值
2、getContextPath()获取当前应用的名称
获取表单传递的参数
可以使用 request.getParameter(paramName) 获取指定名称的参数值
可以使用request.getParameterMap得到一个Map<String, String[]> 然后遍历这个Map
二、jsp内置的9个隐含对象
1、request:HttpServletRequest的一个对象
2、response:HttpServletResponse的一个对象
3、pageContext:PageContext的一个对象
4、session:HttpSession的一个对象
5、application:ServletContext的一个对象
6、config:ServletConfig的一个对象
7、out:JspWriter对象
8、page:Object类型,指向当前JSP对应的Servlet对象的引用
9、exception:声明了isErrorPage="true"时才可以使用
注释
JSP的注释<%-- --%> 在网页上查看源代码时不会显示
HTML的注释<!-- --> 在网页上查看源代码时会显示
三、域对象(作用范围从小到大)
pageContext 属性的作用范围仅限于当前的JSP页面
request 属性的作用范围仅限于用一个请求(在有转发的情况下可以跨页面获取值)
session 属性的作用范围限于一次会话:浏览器打开直到关闭成为一次会话(在此期间会话不会失效)
application 属性的作用范围限于当前WEB应用,是范围最大的属性作用范围
四、请求的转发与重定向
本质区别:
转发 request.getRequestDispatcher(path).forward(request, response);
发出一次请求
request是原来的那个
地址栏不变
只能转发给当前WEB应用的资源
/代表当前WEB应用的根目录(http://localhost:8080/ProjectName/)
重定向 response.sendRedirect(path);
发出两次请求
request对象不是原来的
地址栏变成重定向的地址
可以重定向到任何资源
/代表当前WEB站点的根目录(http://localhost:8080/)
JSP指令
1、page指令
import:指定当前JSP 页面对应的Servlet需要导入的类
<%@page import="java.util.Date"%>
session:取值为true/false ,指定当前页面的session 隐藏变量是否可用 即访问当前页面时 是否一定要生成HttpSession对象
<%@page session="false"%>
errorPage 和 isErrorPage
在可能发生错误的页面可以设置<%@page errorPage="指定的错误显示页面"%>
发生错误时,jsp引擎使用请求转发的方式跳转到错误页面
在错误显示的页面设置<%@page isErrorPage="true"%> 可以使用exception对象 <%=exception%>显示错误信息,一般不建议可以直接访问该页面,可以将该页面放置在WEB-INF目录下
对于tomcat而言。WEB-INF目录下的文件是不能够通过浏览器直接访问的,但是可以通过请求转发访问。
在web.xml中可以配置<error-page>标签
<error-page>
<error-code>404</error-code>
<location>/login.html</location>
</error-page>
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/test.html</location>
</error-page>
contentType
指定当前JSP页面的响应类型,实际调用的是response.setContentType("text/html; charset=UTF-8");
通常情况下,对于JSP页面而言其取值均为text/html; charset=UTF-8 charset指定返回的页面的字符编码是什么,通常取值为UTF-8
pageEncoding
指定当前JSP页面的字符编码,通常情况下该值和contentType中的charset一致
isELIgnored
指定当前JSP页面是否可以使用EL表达式,通常取值为true
2、include指令
静态引入 在源代码级别进行引入 当前JSP页面与静态引入的页面合并为一个Servlet,在被JSP引擎翻译成Servlet过程中合并,不是先合并源文件后再对合并的结果进行翻译
<%@ include file="b.jsp" %>
file属性的值需要使用相对路径
在一个页面定义的变量,在另一个被引入的页面可以访问
方便对重复代码进行重用,可以在jsp文件中放入重复的js/html代码引入,方便重用
动态引入
生成两个Servlet文件 然后通过方法将其包含进来
<jsp:include page="/b.jsp"></jsp:include>
和 request.getRequestDispatcher("/b.jsp").forward(request, response); 相同
<%-- 带参数跳转到指定页面,跳转到的页面可以获取param --%>
<jsp:forward page="/b.jsp">
<jsp:param value="gaosh" name="username"/>
</jsp:forward>
<%-- 带参数包含指定页面,被包含的页面可以获得param --%>
<jsp:include page="/b.jsp">
<jsp:param value="gaosh" name="username"/>
</jsp:include>
中文乱码问题
在JSP页面上输入中文,请求页面后不出现乱码:保证contentType中的charset和pageEncoding的编码一致且支持中文,通常建议取值为UTF-8
获取中文参数:默认参数在传输过程中使用的编码为ISO-8859-1
对于POST请求:只要在获取请求参数之前,调用request.setCharacterEncoding("UTF-8"); 即可
对于GET请求:前面的方式对于GET无效,有两种方法
第一种使用String类的方法,对获得的参数使用UTF-8重新编码
<%
String val = request.getParameter("username");
String username = new String(val.getBytes("iso-8859-1"), "UTF-8");
%>
第二种设置tomcat的server.xml
http://localhost:8080/docs/config/http.html
在eclipse的Servers中找到server.xml文件,在Connector节点中添加useBodyEncodingForURI="true"
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"useBodyEncodingForURI="true"/>
多个请求使用一个Servlet的方法
1、在请求的URL后加?method=xxx,Servlet通过得到的方法名来调用对应的方法
String method = request.getParameter("method");
switch (method) {
case "add":
add(request, response);
break;
case "query":
query(request, response);
break;
case "delete":
delete(request, response);
break;
}
2、使用反射
- 修改Servlet的配置 <url-pattern>*.do</url-pattern> 请求的url为xx.do其中xx代表方法名
// 获取ServletPath:/xx.do
StringservletPath=request.getServletPath();
// 截取字符串,去除/和.do,得到xx字符串
StringmethodName=servletPath.substring(1);
methodName=methodName.substring(0,methodName.length() - 3);
try{
// 利用反射获取methodName对应的方法xx
Methodmethod= getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class);
// 利用反射调用对应的方法
method.invoke(this,request,response);
}catch(Exceptione) {
e.printStackTrace();
}
privatevoidxx(HttpServletRequestrequest, HttpServletResponseresponse){
}
条件查询
对于条件查询,可以将查询的条件字段封装为一个类,然后在getXXX中对字段进行处理
// CriteriaCustomer类的name字段
// 如果为空则值为"%%"
public String getName() {
if (name == null) {
name = "%%";
} else {
name = "%" + name + "%";
}
return name;
}
// 条件查询的方法
@Override
publicList<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) {
Stringsql="SELECT id, name, address, phone FROM customers WHERE name LIKE ? AND address LIKE ? AND phone LIKE ?";
returngetForList(sql,cc.getName(),cc.getAddress(),cc.getPhone());
}
删除
删除时,使用jquery弹出alert确认是否删除,在接受id参数时要注意非法参数的判断
String idStr = request.getParameter("id");
int id = 0;
try {
id = Integer.parseInt(idStr);
customerDAO.delete(id);
} catch (Exception e) {
}
response.sendRedirect("index.jsp");
添加
添加时,如果账号等信息在数据库已经存在,则可以通过转发提示(同一个request)
// 判断用户名是否存在,如果存在则将信息转发至注册页面,并结束方法
if (count > 0) {
request.setAttribute("message", "用户名" + name + "已被占用,请重新选择!");
request.getRequestDispatcher("newcustomer.jsp").forward(request, response);
return;
}
//转发后依旧是同一个request,可以用来回显数据
<tr>
<td>CustomerName:</td>
<td><inputtype="text"name="name"
value="<%=request.getParameter("name") == null ? "" : request.getParameter("name")%>"/></td>
</tr>
重定向与请求转发的区别
(1)重定向方式:刷新,不会重新提交表单数据,而请求转发,在刷新提交表单,会多次提交
(2)在转发的servlet中,重定向不是同一个request,而转发的request的request是同一个
(3)请求转发:只能转发到当前WEB应用资源,而重定向:可以重定向到任何资源,比如www.csdn.net
(4)请求转发只发出了一次请求,而重定向则发出了两次请求,其地址栏为最后的那个地址
更新
- 关于接受id的非法参数的另一种处理方式,如果出现非法参数则跳转到错误页面
String forwardPath = "/error.jsp";
String idStr = request.getParameter("id");
try {
Customer customer = customerDAO.get(Integer.parseInt(idStr));
if (customer != null) {
forwardPath = "/updatecustomer.jsp";
}
request.setAttribute("customer", customer);
} catch (NumberFormatException e) {
}
request.getRequestDispatcher(forwardPath).forward(request, response);
- MySQL在windows下不区分大小写,使用equals比较时区分大小写,需要使用equalsIgnoreCase方法比较
JavaBean在JSP中的应用(了解)
- <jsp:useBean></jsp:useBean>
- <jsp:setProperty/>
- <jsp:getProperty/>
// <jsp:useBean>的用法
<jsp:useBean id="customer" class="com.gs.domain.Customer" scope="session"></jsp:useBean>
// 相当于下面的代码
<%
// 1. 从scope(session)中获取id(customer)属性值,赋值给class(com.gs.domain.Customer)类型的id(customer)变量
Customer customer = (Customer) session.getAttribute("customer");
// 2. 若属性值为null,则利用反射创建一个新对象,把该对象赋值给id(customer),并以id(customer)为属性名添加到scope(session)中
if(customer == null) {
customer = (Customer)Class.forName("com.gs.domain.Customer").newInstance();
session.setAttribute("customer", customer);
}
%>
// 设置customer对象中name的值
<jsp:setPropertyproperty="name"value="gaosh"name="customer"/>
// 相当于下面的代码
<%
customer.setName("gaosh");
%>
// 获取customer对象中name的值
<jsp:getPropertyproperty="name"name="customer"/>
// 相当于下面的代码
<%=customer.getName() %>
<!-- 若property的值为*,省略value属性,则将自动为所有属性赋值为对应的请求参数的值 -->
<!-- 可以通过请求参数赋值 http://localhost:8080/SessionDemo/bean.jsp?name=gaosh&address=shanghai&cardType=visa&card=123 -->
<jsp:setPropertyproperty="*"name="customer"/><br>
name : <jsp:getPropertyproperty="name"name="customer"/><br>
address : <jsp:getPropertyproperty="address"name="customer"/><br>
cardType : <jsp:getPropertyproperty="cardType"name="customer"/><br>
card : <jsp:getPropertyproperty="card"name="customer"/><br>
<jsp:useBeanid="customer2"beanName="com.gs.domain.Customer"
type="java.lang.Object"scope="request"></jsp:useBean>
<%
Objectcustomer2= request.getAttribute("customer2");
if(customer2 == null) {
customer2 = Class.forName("com.gs.domain.Customer").newInstance();
request.setAttribute("customer2", customer2);
}
%>
阅读全文
0 0
- jsp笔记
- JSP笔记
- JSP笔记
- jsp笔记
- JSP笔记
- JSP笔记
- jsp笔记
- JSP 笔记
- JSP笔记
- JSP笔记
- jsp笔记
- JSP 笔记
- JSP笔记
- jsp笔记
- jsp笔记
- jsp笔记
- jsp笔记
- JSP 笔记
- Docker 容器使用
- 2017秋招求职历程总结
- 线索二叉树
- java.lang.NoClassDefFoundError:com/sun/tools/javac/code/TypeTags
- 每日学习流水账
- jsp笔记
- 基于等价类的邮箱注册测试用例设计
- CSS@media查询
- 最小二乘法实现二维多传感器标定
- linux下安装scrpay框架
- Ignatius's puzzle HDU
- C#的值类型和引用类型解析
- Qt 学习之路 2(54):剪贴板
- Java多线程之原子性 volatile、atomicInteger、synchronized测试