欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsp与servlet之间跳转的路径设置

程序员文章站 2022-04-17 11:30:40
...

jsp与servlet之间跳转的路径设置

一、JSP、Servlet中的相对路径
a) 在JSP中

“/”代表站点(服务器)根目录http://127.0.0.1/

b) 在Servlet中

“/”代表Web应用的根目录http://127.0.0.1/JSP_Servlet_Path/

request.getRequestDispatcher("/a/a.jsp").forward(request, response);

相对路径/a/a.jsp中/相对于web应用的根目录,所以相当于请求跳转到绝对路径

http://127.0.0.1/JSP_Servlet_Path/a/a.jsp

response.sendRedirect("/JSP_Servlet_Path/b/b.jsp");

因为重定向中的方法是传递给浏览器,用于重新发送请求的,而在浏览器端“/”代表,相对于站点根目录http://127.0.0.1/,所以这里必须要加上/JSP_Servlet_Path,这样浏览器重新请求的地址为:http://127.0.0.1/JSP_Servlet_Path/b/b.jsp

package com.jsp_servlet_path.rqq;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JSP_Servlet extends HttpServlet {
@Override
public voiddoGet(HttpServletRequest request, HttpServletResponse response)
        throwsServletException, IOException {
           doPost(request,response);
}
@Override
public voiddoPost(HttpServletRequest request, HttpServletResponse response)
             throwsServletException, IOException {
           System.out.println(request.getContextPath());

//请求转发,在服务器端,"/"代表(相对于)web应用http://localhost:8000/JSP_Servlet_Path/

           //所以请求转发的绝对路径为http://localhost:8000/JSP_Servlet_Path/a/a.jsp

//                 request.getRequestDispatcher("/a/a.jsp").forward(request,response);

           //请求重定向,将方法中的地址参数传递给浏览器,让浏览器重新发送请求,

           //"/"代表(相对于)服务器站点http://localhost:8000/

           //所以相当于浏览器重新请求了绝对路径http://localhost:8000/JSP_Servlet_Path/b/b.jsp

           response.sendRedirect("/JSP_Servlet_Path/b/b.jsp");

//                 response.sendRedirect(request.getContextPath()+ "/b/b.jsp");
}
}

二、JSP与Servlet相互访问

package javaBean;

import java.sql.*;

public class DBConnection {
	private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=design";
	private static String userName = "sa";
	private static String userPassword = "123456";
	static Connection conn = null;
	public static Connection getConnection(){
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName, userPassword);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	public static void closeDB(Connection conn, PreparedStatement pstm, ResultSet rs){
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (pstm != null) {
			try {
				pstm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
相关标签: jsp