jsp+servlet+mysql 로그인 등록 실현
7978 단어 HTML
$(document).ready(function(){
$("#kong").click(function(){
$("#zhan").val("");
$("#mima").val("");
});
});
등록 인터페이스:
$(document).ready(function(){
$("#kong").click(function(){
$("#zhan").val("");
$("#mima").val("");
$("#bumen").val("");
});
});
loginservlet:
package com.smxy.zjq.dao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.smxy.zjq.db.DbPool;
/**
* Servlet implementation class LoginServlet
*/
//@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static Connection conn = DbPool.getConnectionFromPool();
static PreparedStatement pstmt = null;
private static ResultSet rs = null;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(" :"+userName+" :"+password);
//System.err.println(userName+";"+password);
int i;
try {
i = search(userName,password);
if(i == 1) {
request.getRequestDispatcher(
"/index.jsp").forward(request, response);
}else if( i == -1){
request.setAttribute("message", " , ");
request.getRequestDispatcher("/Login.jsp").forward(request, response);
}else if(i == 0){
request.setAttribute("message", " !");
request.getRequestDispatcher("/Login.jsp").forward(request, response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int search(String name,String pass) throws SQLException// ,
{
String sql="select * from user where username = '"+name+"'";
pstmt= (PreparedStatement)conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs==null)
{
return 0;
}
if(rs.next()) {
String pas=rs.getString("password");
if(pas.equals(pass)) {
return 1;
}
else
{
return -1;
}
}
return 0;
}
}
registerservlet:
package com.smxy.zjq.dao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.smxy.zjq.Class.User;
import com.smxy.zjq.db.DbPool;
/**
* Servlet implementation class RegisterServlet
*/
//@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static Connection conn = DbPool.getConnectionFromPool();
static PreparedStatement pstmt = null;
private static ResultSet rs = null;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(" :"+userName+" :"+password);
User user = new User(userName,password);
if(savedata(user)){
request.getRequestDispatcher(
"/index.jsp").forward(request, response);
}else{
request.setAttribute("message", " !");
request.getRequestDispatcher("/Register.jsp").forward(request, response);
}
}
public static boolean savedata(User user) {
String insertsql = "insert into user(username,password) values(?,?)";//
try {
int i = 1;
pstmt = conn.prepareStatement(insertsql);
pstmt.setString(i++, user.getUsername());
pstmt.setString(i++, user.getPassword());
int val = pstmt.executeUpdate();
if (val > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
dbpool:
package com.smxy.zjq.db;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbPool {
static DataSource dataSource;
static {
// , c3p0 : 、
dataSource=new ComboPooledDataSource();
}
public static Connection getConnectionFromPool(){
try {
Connection connection=dataSource.getConnection();
return connection;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
XSS 사용해보기이 검증은 자신이 관리하는 가상 머신의 웹 서버에서 수행됩니다. 크로스 사이트 스크립팅 (이하 XSS)을 실제로 시도하고 철저히 이해하는 목적. 이 기사에서는 서버에서 수행하는 처리 내용과 XSS 방법에 대해 설명합...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.