Page Jump and JDBC of Java Web

catalogue

1, Page Jump

Jump in 1.js

2. Jump using Java

·The difference between forwarding and redirection

·Forward

·Redirect

II. Database connection

1. What is JDBC

2. Connect to the database

1, Page Jump

When it comes to page Jump, of course we need pages. Let's write two interfaces first.

login interface: login

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="doLogin.jsp" method="post">
		<p><input name="username" placeholder="Please enter the account number"></p>
		<p><input name="userpwd" placeholder="Please input a password"></p>
		<p><button>Sign in</button></p>
	</form>
	
</body>
</html>

First interface: home

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>Welcome to the home page</h2>
	<h2>welcome <%=request.getParameter("username") %>come back</h2>
</body>
</html>

All the interfaces below jump from the login interface above to the home page

Jump in 1.js

out.print("<script>location.href='home.jsp'</script>")

💌 Login processing interface: doLogin

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//Data transmission in the network uses bytes, which will cause garbled code
	request.setCharacterEncoding("utf-8"); //Set the character encoding in the request to Chinese

	//Get data from the front end
	String name=request.getParameter("username");
	String pwd=request.getParameter("userpwd");

	// Jump location in js href='xxx'
	if("sa".equals(name)&&"123".equals(pwd)){
		out.print("<script>location.href='home.jsp'</script>");
	}else{
		out.print("<script>location.href='login.jsp'</script>");
	}
%>

 

  

js can also jump, but can't get the value.  

2. Jump using Java

There are two ways to jump in Java: forwarding and redirection.

·The difference between forwarding and redirection

  1. Forwarding is the server behavior, and redirection is the client behavior
  2. Forwarding can carry data, but redirection cannot
  3. The forwarding interface path will not change, and the redirection path will change

·Forward

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

💕 Easy to understand

[forward]: you ask your father for money. If your father has no money, he will ask your mother for money and he will give you the money

💞 Picture analysis

 

  💌 Login processing interface: doLogin

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//Data transmission in the network uses bytes, which will cause garbled code
	request.setCharacterEncoding("utf-8"); //Set the character encoding in the request to Chinese

	//Get data from the front end
	String name=request.getParameter("username");
	String pwd=request.getParameter("userpwd");

	//validate logon
	if("sa".equals(name)&&"123".equals(pwd)){
		request.getRequestDispatcher("home.jsp").forward(request, response);
	}else{
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}
%>

 

 

The path stays in the doLogin (login processing) interface, and the user's value can be read.

·Redirect

response.sendRedirect("home.jsp");

💕 Easy to understand

[redirect]: you ask your father for money, but your father has no money. He asks you to ask your mother for money, and your mother will give you the money

💞 Picture analysis

  💌 Login processing interface: doLogin

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//Data transmission in the network uses bytes, which will cause garbled code
	request.setCharacterEncoding("utf-8"); //Set the character encoding in the request to Chinese

	//Get data from the front end
	String name=request.getParameter("username");
	String pwd=request.getParameter("userpwd");

	//validate logon
	if("sa".equals(name)&&"123".equals(pwd)){
		response.sendRedirect("home.jsp");
	}else{
		response.sendRedirect("login.jsp");
	}
%>

 

 

The path jumps to the home interface, and the user's value cannot be read.  

II. Database connection

1. What is JDBC

JDBC is the abbreviation of Java database connection technology, which provides the ability to connect various common databases

 

DriverManager: manage JDBC drivers according to different databases

Connection: it is responsible for connecting to the database and transmitting data

Statement: generated by Connection and responsible for executing SQL statements

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

2. Connect to the database

To connect to the database:

I divided the steps into two big steps

Step 1:

1. Now copy and paste the jar package under the lib package in webapp

 2. Right click the jar package we imported, Build Path, and then Add to Build Path

3. After adding, you can see the following picture

Step 2:

1. When importing drivers (sqlserver,oracle,mysql), be sure to execute build path
OracleDriver only needs to be used to import packages, and then it can be deleted

        Class.forName("oracle.jdbc.driver.OracleDriver");

2. Write connection statement
    String url="jdbc:oracle:thin:@localhost:1521:orcl";
/ / open oracle service: open the service and listen

3. Get connected
    Connection con=DriverManager.getConnection(url, "user name", "user password");    

4. Get precompiled object
    PreparedStatement ps=con.prepareStatement("select * from table name where XX=? and XX =?" ");
Assign values to placeholders
    ps.setString(1, name);
    ps.setString(2, pwd);

5. Get result set (result)
    ResultSet rs=ps.executeQuery();

6. Judge whether the entered user exists in the database [login verification page Jump]

7. Close resources
    if(con!=null&&!con.isClosed()){
        con.close();
    }
    if(ps!=null){
        ps.close();
    }
    if(rs!=null){
        rs.close();
    }

Create a table in the database and insert a piece of data

create table t_user(
       user_name varchar2(20) not null,
       user_pwd varchar2(20) not null
);
insert into t_user(user_name,user_pwd) values('root','root123');
select * from t_user;
commit --Be sure to submit

💌 Login processing interface: doLogin

<%@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"%>
<%

	//Data transmission in the network uses bytes, which will cause garbled code
	request.setCharacterEncoding("utf-8"); //Set the character encoding in the request to Chinese

	//Get data from the front end
	String name=request.getParameter("username");
	String pwd=request.getParameter("userpwd");

	// 1. When importing drivers (sqlserver,oracle,mysql), be sure to execute build path
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");

	// 2. Write connection statement
	String url="jdbc:oracle:thin:@localhost:1521:orcl";
	//Open oracle service: open service and listen
	
	// 3. Get connected
	Connection con=DriverManager.getConnection(url,"scott","zkingedu");
	
	// 4. Get precompiled object
	PreparedStatement ps=con.prepareStatement("select * from t_user where user_name=? and user_pwd=?");
	//Assign values to placeholders
	ps.setString(1, name);
	ps.setString(2, pwd);
	
	// 5. Get result set (result)
	ResultSet rs=ps.executeQuery();
	
	// 6. Judgment
	//validate logon
	if(rs.next()){
		request.getRequestDispatcher("home.jsp").forward(request, response);
	}else{
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}
	
	// 7. Close resources
	if(con!=null&&!con.isClosed()){
		con.close();
	}
	if(ps!=null){
		ps.close();
	}
	if(rs!=null){
		rs.close();
	}

%>

 

 

That's all for this issue. I'll share more knowledge with you in the next issue

Tags: Java Oracle Eclipse html jar

Posted by dsp77 on Mon, 28 Mar 2022 17:10:39 +0300