I tried jsp + jdbc for the first time, and tossed for a long time according to the examples in the book.Just can't connect to the database.So I looked for materials online,Finally found out,The old jar package is directly incompatible with the new database.So I downloaded a new database jdbc package and tried it.Sure enough.Here, share this program with everyone,The program implements user name and password extraction on the web login interface.Then corresponds to the username and password in the database,Thereby, it is determined whether the program is registered or not.

inc.jsp file:

<%@page import="java.sql.connection"%>
<%@page import="java.sql.drivermanager"%>
<%@page import="java.sql.statement"%>
<%@page import="java.sql.resultset"%>
<%@page import="java.sql.resultsetmetadata"%>
string drv="com.mysql.jdbc.driver";
string url="jdbc:mysql://localhost:3306/demo";
string usr="nari";
string pwd="nari";

welcome.jsp file:


login_action.jsp file:

<%@include file="inc.jsp"%>
string username=request.getparameter ("username");
string password=request.getparameter ("password");
if (username == null || password == null) {
  response.sendredirect ("index.jsp");
boolean isvalid=false;
string sql="select * from user where username =" "+ username +" "and password =" "+ password +" "";
out.println ("===>" + sql);
try {
  class.forname (drv) .newinstance ();
  connection conn=drivermanager.getconnection (url, usr, pwd);
  statement stm=conn.createstatement ();
  resultset rs=stm.executequery (sql);
  if (rs.next ()) isvalid=true;
  rs.close ();
  stm.close ();
  conn.close ();
} catch (exception e) {
  e.printstacktrace ();
  out.println (e);
if (isvalid) {
  response.sendredirect ("welcome.jsp");
} else response.sendredirect ("index.jsp");
if (username.endswith ("a")) response.sendredirect ("welcome.jsp");
else response.sendredirect ("index.jsp");
* /%>

index.jsp file:

<%@page language="java" import="java.util. *" Pageencoding="utf-8"%>
string path=request.getcontextpath ();
string basepath=request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";
    <base href="<%= basepath%>" />
    <title>my jsp "login.jsp" starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="description" content="this is my page">
  mithis is my jsp page.<br>
<form name="form1" action="login_action.jsp" method="post">
<table width="200" border="1">
  <td colspan="2">Login window</td>
  <td><input type="text" name="username" size="10" /></td>
  <td><input type="password" name="password" size="10" /></td>
  <td colspan="2"><input type="submit" name="submit" value="login">
  <a href="register.jsp">Register new user</a></td>

The program uses tomcat for publishing,myeclipse for editing and debugging

  • Previous Timestamp and time conversion (phpnet accurate to milliseconds)
  • Next Win Server 2008 R2 with PowerShell