web書城專案-servlet實現
一、資料庫連線c3p0--工具類
public class JDBCUtils {
//資料來源是一個重量級物件,在專案中有一個例項即可,所以宣告成靜態成員
private static DataSource dataSource = new ComboPooledDataSource ("webDataSource" );
public static Connection getConnection(){
Connection connection = null;
try {
connection = dataSource . getConnection() ;
} catch ( SQLException e ) {
e . printStackTrace() ;
}
return connection;
}
/**
* 釋放資料庫連線
* @param connection
*/
public static void releaseConnection (Connection connection ) {
try {
if (connection != null ){
connection . close() ;
}
} catch ( SQLException e ) {
e . printStackTrace() ;
}
}
}
二、 規定 Dao實現類的功能,上層程式根據介面功能使用 Dao實現類。當實現類發生改變時,只要介面不變,上層的呼叫方式就不需要改變。
DAO介面定義:
public interface DAO <T > {
//實現資料庫的增刪改
void update (Connection conn ,String sql ,Object ... params) ;
//查詢資料庫返回單一物件
T getBean (Connection conn ,String sql ,Object ... params) ;
//查詢資料庫返回物件的集合
List < T> getBeanList (Connection conn ,String sql ,Object ... params) ;
}
UserDao介面定義:
public interface UserDAO {
//1.根據使用者名稱和密碼獲取使用者物件
User getUser (User user) ;
//2.根據使用者名稱獲取使用者物件
User getUserByName (User user) ;
//3.儲存使用者物件
void saveUser (User user) ;
}
三、DAO實現類
DaoImpl實現類:
// Dao基類,使用時通過子類繼承,傳入泛型引數。建立子類物件使用。
public class DaoImpl <T > implements DAO <T >{
//使用QueryRunner物件
private QueryRunner runner =new QueryRunner() ;
//定義泛型物件
private Class< T > beanType =null;
public DaoImpl () {
Type superClass =this. getClass() . getGenericSuperclass() ;
if (superClass instanceof ParameterizedType){
ParameterizedType pt =( ParameterizedType) superClass ;
Type [] types = pt .getActualTypeArguments ();
beanType =( Class) types [0 ]; //獲取泛型引數T,獲得實際型別值對應的Class類物件
}
}
@Override
public void update( Connection conn, String sql, Object... params ) {
try {
runner .update (conn , sql, params) ;
} catch ( Exception e ) {
e . printStackTrace() ;
}
}
@Override
public T getBean( Connection conn, String sql, Object... params ) {
T t =null ;
try {
t = runner . query( conn, sql , new BeanHandler <T >( beanType) , params) ;
} catch ( Exception e ) {
e . printStackTrace() ;
}
return t;
}
@Override
public List< T > getBeanList (Connection conn , String sql , Object ... params ) {
List < T> beanList =null;
try {
beanList = runner . query( conn, sql , new BeanListHandler <>( beanType) ,params );
} catch ( Exception e ) {
e . printStackTrace() ;
}
return beanList;
}
}
UserDaoImpl實現類:
public class UserDaoImpl extends DaoImpl < User> implements UserDAO {
@Override
public User getUser (User user) {
Connection conn =JDBCUtils . getConnection() ;
String sql ="SELECT
user_id userId,user_name userName,user_pwd pwd,user_email email FROM `user`"
+ "
WHERE user_name=? AND user_pwd=?" ;
User querUser = this. getBean( conn , sql , user. getUserName() , user. getPwd()) ;
JDBCUtils . releaseConnection( conn) ;
return querUser;
}
@Override
public User getUserByName (User user) {
Connection conn = JDBCUtils .getConnection ();
String sql ="SELECT
user_id userId,user_name userName,user_pwd pwd,user_email email FROM `user`"
+ "
WHERE user_name=? " ;
User queryUser = this. getBean( conn, sql , user. getUserName()) ;
JDBCUtils . releaseConnection( conn) ;
return queryUser;
}
@Override
public void saveUser( User user) {
Connection conn =JDBCUtils . getConnection() ;
String sql = "INSERT
INTO `user` (user_name,user_pwd,user_email) VALUES(?,?,?)";
System . out. println( user .getUserName ());
this. update( conn , sql , user. getUserName() , user. getPwd() , user. getEmail()) ;
JDBCUtils . releaseConnection( conn) ;
}
}
測試UserDAO
public class UserDaoTest {
private UserDaoImpl userDao =new UserDaoImpl() ;
@Test
public void testGetUser() {
User user =userDao . getUser( new User( null, "tzq", "123" , null ));
System . out. println( user) ;
//User [userId=1, userName= tzq, pwd=123, email=123@123.com]
}
@Test
public void testGetUserByName() {
User user =userDao . getUserByName( new User ( null, "admin" , null, null)) ;
System . out. println( user) ;
//User [userId=2, userName= admin, pwd=122, email=222@11.com]
}
@Test
public void testSaveUser() {
User user = new User( null, "譚正強", "123456", "tan@163.com" );
userDao .saveUser (user );
System . out. println( "資料儲存成功" );
}
}
四、定義判斷能否登陸的介面方法
public interface UserServiceInter {
//根據使用者名稱、密碼校驗使用者能否登陸
boolean login (User user) ;
//根據使用者名稱驗證能否註冊,如果可以註冊,則將User物件儲存到資料庫中
boolean regist (User user) ;
}
五、建立UserServiceInter 介面的實現類UserServiceImpl
public class UserServiceImpl implements UserServiceInter {
private UserDAO userDao =new UserDaoImpl() ;
@Override
public boolean login( User user) {
User queryuser = userDao. getUser( user) ;
return queryuser!= null;
}
@Override
public boolean regist( User user) {
User userByName = userDao. getUserByName (user );
if (userByName ==null ){
userDao .saveUser (user );
return true ;
} else{
return false ;
}
}
}
測試UserService
public class UserServiceTest {
private UserServiceInter userService = new UserServiceImpl ();
@Test
public void testLogin() {
User user =new User ( null, "tzq" , "123" , null );
boolean login = userService .login (user );
if (login ){
System . out. println( "登陸成功" );
} else{
System . out. println( "登陸失敗" );
}
}
@Test
public void testRegist() {
User user = new User( null, "tzqq", "123", "tz@126.com" );
boolean regist = userService .regist (user );
if (regist ) {
System . out. println( "註冊成功" );
} else{
System . out. println( "註冊失敗" );
}
}
}
六、建立登陸以及註冊Servlet
登陸處理邏輯:接受請求→處理請求→轉發
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L ;
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
//1.獲取請求引數
String userName = request. getParameter( "userName" );
String pwd = request .getParameter ("pwd" );
User user = new User( null, userName , pwd , null );
//2.處理請求引數
UserServiceImpl userServiceImpl = new UserServiceImpl ();
boolean login = userServiceImpl . login( user) ;
//3.派發頁面
if (login ) {
//如果登入成功轉發到loginSuccess.html
//①轉發是伺服器端完成,所以路徑由伺服器解析
//②第一個“/”代表當前Web應用的根目錄
request . getRequestDispatcher( "/client/user/loginSuccess.html" ). forward( request , response );
} else{
//如果登入失敗重定向到loginError.html
//①重定向中指定的地址是瀏覽器解析
//②第一個“/”代表當前伺服器,還需要進一步指定當前Web應用的虛擬路徑,通常為當前Web工程的名字
response . sendRedirect( "/BookStore02/client/user/loginError.html" );
}
}
}
註冊處理邏輯:接受請求→處理請求→轉發
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L ;
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
//1.獲取請求引數
String userName = request. getParameter( "userName" );
String pwd = request .getParameter ("pwd" );
String email = request .getParameter ("email" );
User user = new User( null, userName , pwd , email) ;
//2.處理請求引數
UserServiceImpl userServiceImpl = new UserServiceImpl ();
boolean regist = userServiceImpl . regist( user) ;
//3.派發頁面
if (regist ) {
//如果註冊成功轉發到registSuccess.html
request . getRequestDispatcher( "/client/user/registSuccess.html" ). forward( request , response );
} else{
//如果註冊失敗重定向到registError.html
response . sendRedirect( "/BookStore02/client/user/registError.html" );
}
}
}
專案圖展示:
相關文章
- web專案中web.xml的servlet和servletmapping的解讀WebXMLServletAPP
- JavaWeb尚矽谷書城專案思維導圖JavaWeb
- 使用Servlet實現檔案下載Servlet
- maven非web專案整合mbatis實現CRUDMavenWebBAT
- 小米商城專案(JSP+Servlet專案)JSServlet
- java web專案 使用elfinder 實現檔案管理器JavaWeb
- Servlet的實現Servlet
- Day116.簡單書城專案---第一階段 -JavaWebJavaWeb
- 07#Web 實戰:實現 GitHub 個人主頁專案拖拽排序WebGithub排序
- servlet實現下載功能Servlet
- ssts-hospital-web-master專案實戰記錄三十一:專案遷移-核心模組實現(useDeviceDriver)WebASTdev
- ssts-hospital-web-master專案實戰記錄三十二:專案遷移-核心模組實現(useFlow)WebAST
- springmvc學習指南 之---第27篇 spring如何實現servlet3.0無web.xml 配置servlet物件的SpringMVCServletWebXML物件
- Web專案開發介紹及實戰專案介紹Web
- web 報表專案中想嵌入地圖?用什麼工具實現?怎樣實現?Web地圖
- 2023 年的 Web Worker 專案實踐Web
- mvn 建立java專案 web專案JavaWeb
- Vue + Spring Boot 專案實戰(十九):Web 專案優化解決方案VueSpring BootWeb優化
- 專案如何實現限流?
- 如何實現專案管理專案管理
- ssts-hospital-web-master專案實戰記錄五十:專案遷移-業務頁面實現(主框架-檔案結構)WebAST框架
- 【javaWeb】第51天—— WEB實戰專案三JavaWeb
- 【javaWeb】第52天—— WEB實戰專案四JavaWeb
- SpringBoot 中的 Servlet Web 容器Spring BootServletWeb
- Java Servlet session實現登入退出JavaServletSession
- JAVAEE_Servlet_24_HttpSession實現原理JavaServletHTTPSession
- 實驗專案四:圖書管理系統
- 專案出現 The superclass “javax.servlet.http.HttpServlet“ was not found on the Java Build Path 解決方法JavaServletHTTPUI
- ssts-hospital-web-master專案實戰記錄三十三:專案遷移-核心模組實現(useDeviceDriver-刷卡器)WebASTdev
- ssts-hospital-web-master專案實戰記錄三十三:專案遷移-核心模組實現(useDeviceDriver-文件印表機)WebASTdev
- idea建立web專案IdeaWeb
- ssts-hospital-web-master專案實戰記錄三十三:專案遷移-核心模組實現(useDeviceDriver-標籤印表機)WebASTdev
- Django實現web端tailf日誌檔案DjangoWebAI
- 用一個檔案,實現迷你 Web 框架Web框架
- Java Web(5)-Servlet詳解(下)JavaWebServlet
- Java Web(5)-Servlet詳解(上)JavaWebServlet
- ssts-hospital-web-master專案實戰記錄三十三:專案遷移-核心模組實現(useDeviceDriver-身份證讀卡器)WebASTdev
- ssts-hospital-web-master專案實戰記錄三十三:專案遷移-核心模組實現(useDeviceDriver-非接讀卡器)WebASTdev
- Servlet5—IDEA專案配置,解決no artifacts的warningsServletIdea