Javaweb个人笔记

JSP笔记

2017-02-27  本文已影响32人  fanyank

title: JSP笔记
date: 2017-02-24 21:51:49
tags: JSP


JSP&Servlet 笔记

命名规则

文件位置

Servlet和JSP之间传值的两种方式

  1. 重定向跳转
    • 修改地址栏,在浏览器端工作
    • 传值只能依靠URL重写,且仅能是String
    • resp.sendRedirect(“/home.jsp?error=1”);
  2. 请求转发跳转
    • 地址栏不变,在服务器端工作
    • 依靠req.setAttibute()传值,值可以为任意类型
    • 缺点为会产生重复提交
req.getRequestDispatcher("/WEB-INF/views/webjsp/list.jsp").forward(req,resp);

两个Servlet之间的通信只能通过session,但可以通过session也可以间接地创建JSP和Servlet之间的通信

//在web.xml中配置
<web-app...>
        <servlet>
      ...
    </servlet>
        <session-config>
       <session-timeout>20</session-timeout>
    </session-config>
</web-app>
//也可以单独给某一个servlet中的seesion设置超时值,注意是以秒来计算
session.setMaxInactiveInterval(20*60);

配置上下文参数和servlet参数:

//web.xml中配置文件
        <servlet>...</servlet>
        <context-param>
        <param-name>mainEmail</param-name>
        <param-value>fanyank@126.com</param-value>
    </context-param>
//servlet中调用上下文参数
getServletContext().getInitParameter("mainEmail");
//或者...
ServletContext context = getServletContext();
context.getInitParameter("mainEmail");
//web.xml中配置文件
<servlet>
        <servlet-name>...</servlet-name>
        <servlet-class>...</servlet-class>

        <init-param>
            <param-name>adminEmail</param-name>
            <param-value>fanyank@126.com</param-value>
        </init-param>
</servlet>
//servlet中的调用
getServletConfig.getInitParameter("adminEmail");
//调用过程:
//容器初始化一个servlet时,会为这个容器创建唯一的一个ServletConfig,容器从DD中读取初始化参数并把键值交给ServletConfig,再把ServletConfig传递给servlet中的init()方法。

为防止出现错误类型为500的错误:

if(isbn == null || "".equals(isbn)){
            resp.sendError(404);
        }

将页面上请求的表单写入数据库时应采用UTF-8编码防止写入乱码的情况

req.setCharacterEncoding("UTF-8");
//如果通过URL传入中文,则需进行编码
String tagTypeName = new String(req.getParameter("newTagTypeName").getBytes("ISO-8859-1"), "UTF-8");

搜索功能应通过URL重写来传值

删除功能需要二次确认,然后通过JS跳转到Servlet

$(".del").click(function () {

                if(confirm("确定要删除吗")) {
                    var id = $(this).attr("rel");
                    window.location.href = "/product/del?id=" + id;
                }

            });

JSTL 和 EL表达式

// <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
//
// <c:forEach items="${names}" var="item">
//         <li>${item}</li>
// </c:forEach>
//
// <c:choose>
//         <c:when test="${name == 'Alex'}">
//             <h1>Hello,Alex</h1>
//         </c:when>
//         <c:when test="${name == 'Tom'}">
//             <h1>Hello,Tom</h1>
//         </c:when>
//         <c:otherwise>
//             <h1>Hello,Other</h1>
//         </c:otherwise>
// </c:choose>
//
// <c:if test="${12 > 2}">
//         <h1>Hehe</h1>
// </c:if>
// <h1> EL表达式  page->request->session->application </h1>
//     <h1> ${name} </h1>
//     <h1> ${user.id} </h1>
//     <h1>${user.username}</h1>
//     <h1>${user.niceName}</h1>
//     <h1>${requestScope.name}</h1>
//     <h1>${applicationScope.name}</h1>
//     <h1>${1+1}</h1>
//     <h1>${name == 'Alex'}</h1>
//     <h1>${name eq 'Alex'}</h1>
//     <h1>${12 >= 2}</h1>
//     <h1>${empty name}</h1>
//     <h1>${not empty name}</h1>

JSP

  1. 如果需要对被包含的jsp文件内容进行定制,那么就只能使用jsp标准动作了
//使用<jsp:param>定制包含内容
// <jsp:include page="header.jsp">
//        <jsp:param name="subTitle" value="This is value!" />
// </jsp:include>
//被包含的header.jsp
<em>${param.subTitle}</em>
//使用<c:import>可以达到同样的效果
// <c:import url="header.jsp">
//        <c:param name="subTitle" value="This is value" />
// </c:import>

动作中也可以使用EL表达式

url中文传值乱码

将请求值设为UTF-8编码

String tagName = new String(req.getParameter("tagName").getBytes("ISO-8859-1"),"UTF-8");

EL隐式对象

EL表达式示例,${person.name},就上式来说,点运算符左边可以是一个Map或一个Bean,点运算符右边可以是一个映射键或者Bean性质(${getName()}).

建立自己的错误界面

如果用户访问你的网站出现了一些错误,并且不希望访问你网站的用户看到你的栈跟踪记录,也不想让用户得到一个404的错误代码,那么就可以用一个优雅的方式去解决这些问题。
DD中(web.xml) 中进行一下配置:

<error-page>
    //根据HTTP状态声明错误页面
    <error-code>404</error-code>
    //根据抛出错误类型来声明错误页面
    <exception-type>java.lang.ArithmeticException</exception-type>
    <location>/notFound.jsp</location>
</error-page>
上一篇 下一篇

猜你喜欢

热点阅读