总结jsp&servlet中代码跳转的部分写法及概念

1.功能:进行jsp页面就自动跳转到 "IndexServlet"运行相应的servlet中代码。例如:从数据库中查询,需要在index.jsp中显示的默认内容。

<body>

    <jsp:forward page="/client/IndexServlet"></jsp:forward>

 </body>

2.功能:若用户点"登录"按键,则系统向"/client/LoginServlet"发请求。

            若用户点"注册"按键,则系统跳转到"/client/register.jsp"页面

<form action="/client/LoginServlet" method="post">
    用户名:<input type="text" name="name" style="width:50px">
    密码:<input type="password" name="password" style="width:50px">
    <input type="submit" value="登录">
   <input type="button" value="注册" οnclick="
javascript:window.location.href='${pageContext.request.contextPath }/client/register.jsp'">
    </form>

3.script 函数重定位

<script type="text/javascript">
        functiongotopage(
currentpage){
 window.location.href='${pageContext.request.contextPath}/servlet/ListCustomer?currentpage='+currentpage+  '&testvalue2='"${pagebean.pagesize}";
}

    </scripte>

4.from表单提交

<form action="${pageContext.request.contextPath}/manager/CategoryServlet?method=add"method="post">

5.将处理结果在指定位置显示

<a href="${pageContext.request.contextPath}/servlet/AddCustomerServlet" target="main">添加客户</a> 

6.带多个参数

<a href="${pageContext.request.contextPath }/manager/OrderServlet?method=update&order_id=${order.id}">确认发货</a>

7.取消href原连接

 <a href="javascript:void(0)onclick="deleteitem(${entry.key})">删除</a>

8.部分表达的含义

${pageContext.request.contextPath}    适用于jsp中,返回的结果是web应用,如/day22,对照第4,5条。

request.getContextPath()  与this.getServletContext().getContextPath()    返回的结果都一样,都是web应用,不知道两种有何区别。适用于暂只知道servlet中,

this.getServletContext().getRealPath("/client/listcart.jsp"),返回的是如:C:\tomcat7\webapps\day22\client\listcart.jsp

其实对request.getContextPath() ,this.getServletContext().getContextPath(),this.getServletContext().getRealPath ("/client/listcart.jsp")三者还是有些模糊。

9.对于地址的写法要分清服务器与浏览器端

    第一步:/     
   
第二步:判断地址给谁用
       
若地址是服务器使用,则”/“代表当前web应用(WebRoot
       
若地址是浏览器使用,则”/“代表网址    

request.getRequestDispatcher("/form1.html").forward(request, response);    //服务器


response.sendRedirect("/day06/client/listcart.jsp");//浏览器         //day06可用request.getContextPath()下面两种代

response.sendRedirect(request.getContextPath()+"/client/listcart.jsp")

response.sendRedirect(this.getServletContext().getContextPath()+"/client/listcart.jsp")


this.getServletContext().getRealPath("/form1.html");//服务器

this.getServletContext().getResourceAsStream("/form1.html");//服务器

<ahref="/day06/from1.html"></a>//浏览器    /day06可用${pageContext.request.contextPath }代

<formaction="/day06/form1.html">//浏览器

10.测试

1.System.out.println(request.getContextPath());
2.System.out.println(this.getServletContext());
3.System.out.println(this.getServletContext().getContextPath());
4.System.out.println(this.getServletContext().getRealPath("/client/listcart.jsp"));

5.System.out.println(this.getServletContext().getResourceAsStream("/client/listcart.jsp"));

6.${pageContext.request.contextPath}

输出:

1./day22
2.org.apache.catalina.core.ApplicationContextFacade@1525451
3./day22
4.C:\tomcat7\webapps\day22\client\listcart.jsp

5.java.io.ByteArrayInputStream@1daf3d

6./day22



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值