java web (page jump mode & jdbc) three

 

Table of contents

1. The page jump method

1.Jump in js

2. Jump in Java

(1) Forwarding (acting on the client)

(2) Redirection

 Two, jdbc concept carding

 Third, connect to the database

Database connection steps:

case:


1. The page jump method

1.Jump in js

<script>localtion.href='home.jsp'<script>
//注意:一定要写在<script></script>中

2. Jump in Java

(1) Forwarding (acting on the client)

The function of forwarding: realize the sharing of request data in the process of multiple page interaction

Implementation of forwarding:

  • RequestDispatcher object
  • forward() method

code show as below:

request.getRequestDispatcher("xxx.jsp").forward(request,response);

The diagram is as follows:

The client sends a request to the server, the server 1 has no data, it goes to the server 2, the server 2 sends it to the server 1, and then responds to the client. In short, forwarding generates a request (forwarding data can be carried).

(2) Redirection

The response object is used to respond to customer requests and output information to the client 

code show as below:

response.sendRedirect("xxx.jsp");

Direct the request to a different URL.

Graphic display:

 The client sends a request to server 1, and after server 1 has no data, the client sends a request to server 2 again, and so on, until the data is obtained, a total of two requests are sent in the figure.

The difference between forwarding and redirection:

  • Forwarding works on the server side, and the submission information is passed between multiple pages through the forward method.
  • Forwarding is the transfer of control within the server, and the address bar of the client browser will not display the redirected address
  • Redirection works on the client side by requesting a new address to achieve page redirection
  • Redirection is to re-request the address through the browser, and the redirected address can be displayed in the address bar

 Two, jdbc concept carding

JDBC is the abbreviation of Java Database Connection Technology, which provides the ability to connect to various commonly used databases

 JDBC API (establish connection with database, execute SQL statement, process result)

DriverManager: Manage JDBC drivers according to different databases  

Connection: responsible for connecting to the database and taking on the task of transferring data    

Statement: Generated by Connection and responsible for executing SQL statements  

ResultSet: responsible for saving the query results generated after the Statement is executed

 Third, connect to the database

Database connection steps:

Prepare a specific jar package

 And put the jar package in the lib file under the WEB-INF file

Note: Be sure to open the data service before importing the package. Here we use Oracle

Steps: win+x, g->service in application management->service->open the following two services

 

 The steps to connect data are as follows:

   
      //1.导入驱动(SQLserver,oracle,mysql)
      <%@page import="oracle.jdbc.driver.OracleDriver"%>
      //OracleDriver
      //注意:在此之前一定执行build path
      Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
      //2.连接语句
      //sqlserver的连接语句
      //jdbc:sqlserver:localhost:1433;databasename=ss
      String URL="jdbc:oracle:thin:@localhost:1521:orcl";
      //3.获得连接
      Connection con=DriverManager.getConnection(URL, "scott", "sa123");
      //4.获得预编译对象(执行对象)
      PreparedStatement ps=con.prepareStatement(sql语句);//?指的是占位符
      //5.占位符赋值
      ps.setString(占位符位,xx);
      //6.获得结果集
      ResultSet rs=ps.executeQuery();
      //7.判断
      if(rs.next()){
    	  request.getRequestDispatcher("xxx.jsp").forward(request, response);
      }else{
    	  response.sendRedirect("xxx.jsp");
      }
      //8.关闭资源
      if(con!=null&&!con.isClosed()){
    	  con.close();
      }
      if(ps!=null){
    	  ps.close();
      }
      if(rs!=null){
          rs.close();
      }

case:

Simple login case, the code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
	<from action="doLogin.jsp" method="post">
	<p>
		<input name="username" placeholder="请输入名字">
	</p>
	<p>
		<input name="password" placeholder="请输入密码">
	</p>
    </p>
		<button>登录</button>
	</from>
</body>
</html>

Of course, it is necessary to create a database before execution

Here we use oracle to create a simple table

CREATE TABLE T_USER(
 UUID NUMBER PRIMARY KEY,
 UNAME VARCHAR2(20) NOT NULL,
 UPWD VARCHAR2(20) NOT NULL
)
commit;

Note: Here we must submit the data after creating the table (commit).

Connect to the database code:

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
      //网络中的数据都是字节 如果是文字就会产生乱码的情况
      request.setCharacterEncoding("UTF-8");//将请求中的字符全部编码设置为中文
       
      //获取前端数据
      String name=request.getParameter("username");//名字
      String password=request.getParameter("password");//密码
      
      //导入驱动(SQLserver,oracle,mysql)
      //OracleDriver
      //注意:在此之前一定执行build path
      Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
      //连接语句
      //sqlserver的连接语句
      //jdbc:sqlserver:localhost:1433;databasename=ss
      String URL="jdbc:oracle:thin:@localhost:1521:orcl";
      //获得连接
      Connection con=DriverManager.getConnection(URL, "scott", "sa123");
      //获得预编译对象(执行对象)
      PreparedStatement ps=con.prepareStatement("SELECT * FROM T_USER WHERE UNAME=? AND UPWD=?");//?指的是占位符
      //占位符赋值
      ps.setString(1,name);
      ps.setString(2,password);
      //获得结果集
      ResultSet rs=ps.executeQuery();
      //判断
      if(rs.next()){
    	  request.getRequestDispatcher("home.jsp").forward(request, response);
      }else{
    	  response.sendRedirect("login.jsp");
      }
      //关闭资源
      if(con!=null&&!con.isClosed()){
    	  con.close();
      }
      if(ps!=null){
    	  ps.close();
      }
      if(rs!=null){
          rs.close();
      }
      
      
%>

home page code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main</title>
</head>
<body>
     <h1>我是首页</h1>
     <h2>欢迎 <%request.getParameter("username"); %>login</h2>
</body>
</html>

Summary: The key to this issue is to be proficient in how to connect to the database! The next issue will bring you code optimization!

Guess you like

Origin blog.csdn.net/m0_67376124/article/details/123797159