『壹』 求用java編登錄頁面,可以連接MySql 資料庫
第一步:創建一個查詢過程,因為在登錄時要根據用戶名查詢用戶密碼
此步要用到pl/sql編程知識,代碼如下:
create or replace procere sel_user(uname in varchar2,pass out varchar2) is
begin
select users.password into pass from users where users.username=uname and rownum = 1;
end;
第二步:編寫登錄頁面(login.java)(採用純java+servlet編寫)
//login.java如下
package cn.hnu;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class testhtml extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=gbk");
try {
PrintWriter pw = resp.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>");
pw.println("用戶登錄");
pw.println("</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>用戶登錄</h1>");
pw.println("<hr>");
pw.println("<form method=post action=loginCl>");
pw.println("用戶名:<input type=text name=userName><br>");
pw.println("密  碼:<input type=password name=password><br>");
pw.println("<input type=submit value=登錄>");
pw.println("<input type=reset value=重置>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}
}
第三步:編程成功登錄頁面(wel.java) //wel.java如下,它主要用於用戶正常登錄後顯示信息給用戶
package cn.hnu;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Wel extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//防止用戶非法登錄
HttpSession hs = req.getSession();
String s = (String)hs.getAttribute("pass");
if(s == null){
resp.sendRedirect("login");
}
PrintWriter pw = resp.getWriter();
pw.write("welcome,hello");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}
}
第四步:編寫login處理頁面(loginCl.java)
package cn.hnu;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginCl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String u = req.getParameter("userName");
String p = req.getParameter("password");
//查詢資料庫
String pa=null;
Connection ct = null;
CallableStatement cs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
ct = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle",
"scott", "tiger");
cs = ct.prepareCall("{call sel_user(?,?)}");
cs.setString(1, u);
cs.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
cs.execute();
pa = cs.getString(2);
System.out.println("u=" + u + " p=" + pa);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (cs != null) {
cs.close();
}
if (ct != null) {
ct.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//驗證用戶信息是否合法
if (p.equals(pa)) {
HttpSession hs = req.getSession(true);//防止用戶非法登錄
hs.setAttribute("pass", "OK");
resp.sendRedirect("wel");
} else {
resp.sendRedirect("login");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(req, resp);
}
}
親,sql可以換成MySQL
這個沒關系的,別的都可以照搬來用