发布于 2015-05-19 13:55:04 | 85 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的JSP由浅入深,程序狗速度看过来!

JSP JAVA公司的WEB开发编程语言

JSP全名为Java Server Pages - java服务器页面是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML(标准通用标记语言的子集)文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件,后缀名为(*.jsp)。


在web程序中,经常是由多个Servlet来完成请求.RequestDispatcher接口就是为了多个Servlet之间的调整而实现的. 该接口可以用httpSerbletRequest的getRequestDispatcher()方法取得.调用时指定跳转的URL地址即可完成跳转动 作.RequestDispatcher接口有两种方法实现跳转

一.include方法

本示例不需要前台页面,直接通过地址访问.

IncludeMethod.java代码

package com.gys;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name="demo",
        urlPatterns={"/include"}
    )
public class IncludeMethod extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print(" <h1>第一个servlet</h1>");
        RequestDispatcher dispatcher=request.getRequestDispatcher("/includeSecond");
        dispatcher.include(request, response);
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}

IncludeMethod2.java

package com.gys;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(
        name="include2",
        urlPatterns={"/includeSecond"}
        )
public class IncludeMethod2 extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("这是第二个servlet(被包涵在第一个servlet里面)");
    }
}

测试结果:

二.forward()

forwardMethod.java代码

package com.gys2;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name="forwardMethod",
        urlPatterns={"/test"}
        )

public class forwardMethod extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/test.html").forward(request, response);
    }
}

运行结果是这个样子的:



相关阅读 :
jsp中 RequestDispatcher接口的两种跳转:forward()和include()
jsp中获得路径的两种方法和获得url路径的方法(推荐)
两个JSP页面父页面获取子页面内容的两种方法
JSP response对象实现文件下载的两种方式
JSP隐含对象response实现文件下载的两种方法
JSP对浏览器发送来的数据进行重新编码的两种方式
将html页改成jsp的两种方式
jsp中页面之间的跳转forward与sendRedirect的区别
JSP中操作数据库的常用SQL标签用法总结
实例解析JSP中EL表达式的各种运用
JSP文件下载功能的4种方法
jsp跳转getRequestDispatcher()和sendRedirect()的区别
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务