我是靠谱客的博主 酷炫冬天,最近开发中收集的这篇文章主要介绍简单的servlet mvc实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

结构图

id="iframe_0.5173634919337928" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg153.ph.126.net%2FXCHsvqoXwZ1ZOsH-pwcVDg%3D%3D%2F3033455824012355511.jpg&maxWidth=618&origin=http://www.cnblogs.com&iframeId=iframe_0.5173634919337928" frameborder="0" scrolling="no" height="281" style="margin: 0px; padding: 0px; border-style: none; width: 230px;">
 
数据库设计
 
id="iframe_0.07178152771666646" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fimg840.ph.126.net%2Fk7iCStV_tAFP5MALtXz_vQ%3D%3D%2F797137134046663602.jpg&maxWidth=618&origin=http://www.cnblogs.com&iframeId=iframe_0.07178152771666646" frameborder="0" scrolling="no" height="320" style="margin: 0px; padding: 0px; border-style: none; width: 230px;">

 

 

 

  

 

1、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>

 <servlet>
        <servlet-name>loginServlet</servlet-name>
        <servlet-class>org.zxj.mvcdemo.servlet.LoginServlet</servlet-class>
    </servlet>
 

 
  <servlet-mapping>
        <servlet-name>loginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>   
</web-app>

 

 2、User

 

package org.zxj.mvcdemo.vo;

public class User{
 private String userid;
 private String name;
 private String password;
 public void setUSerid(String userid){
  this.userid = userid;  
 }
 public void setName (String name){
  this.name = name;
 }
 public void setPassword(String password){
  this.password = password;
 }
 public String getUserid(){
  return this.userid;
 }
 public String getName(){
  return this.name;  
 }
 public String getPassword(){
  return this.password;
 }
 
}

 

 3、DatabaseConnection

 

package org.zxj.mvcdemo.dbc;

import java.sql.*;

public class DatabaseConnection{

 private static final String DBDRIVER = "com.mysql.jdbc.Driver";
 private static final String DBURL = "jdbc:mysql://localhost:3306/zxj";
 private static final String DBUSER = "root";
 private static final String DBPASSWORD ="pf";
 private static Connection con = null;
 public DatabaseConnection() throws Exception{
  try{
   Class.forName(DBDRIVER);
   this.con = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
  }catch(Exception e){
   throw e;
  }
 }
 public Connection getConnection(){
  return this.con;
 }
 public void close() throws Exception {
  if(this.con != null){
   try{
    this.con.close();
   }catch(Exception e){
    throw e;
   }
  }
 }
}

 

 4、IUserDAO

 

package org.zxj.mvcdemo.dao;

import org.zxj.mvcdemo.vo.User;

public interface IUserDAO{
 public boolean findLogin(User user) throws Exception;
}

 

5、UserDAOImpl

 

package org.zxj.mvcdemo.dao.impl;

import org.zxj.mvcdemo.vo.User;
import org.zxj.mvcdemo.dao.*;
import java.sql.*;

public class UserDAOImpl implements IUserDAO{
 private Connection conn = null;
 private PreparedStatement pstmt = null;
 public UserDAOImpl(Connection conn){
  this.conn = conn;
 }
 public boolean findLogin(User user) throws Exception{
  boolean flag = false;
  String sql = "select name from user where userid= ? and password = ?";
  this.pstmt = this.conn.prepareStatement(sql);
  this.pstmt.setString(1,user.getUserid());
  this.pstmt.setString(2,user.getPassword());
  ResultSet rs = this.pstmt.executeQuery();
  if(rs.next()){
   user.setName(rs.getString("name"));
   flag = true;
  }
  this.pstmt.close();
  return flag;
 }
}

 

6、UserDAOProxy

 

 package org.zxj.mvcdemo.dao.proxy;

import org.zxj.mvcdemo.vo.User;
import org.zxj.mvcdemo.dbc.*;
import org.zxj.mvcdemo.dao.*;
import org.zxj.mvcdemo.dao.impl.*;
import java.sql.*;

public class UserDAOProxy implements IUserDAO{
 private DatabaseConnection dbc = null;
 private IUserDAO dao = null;
 private PreparedStatement pstmt = null;
 public UserDAOProxy(){
  try{
   this.dbc = new DatabaseConnection();
  }catch(Exception e){
   e.printStackTrace();
  }
  this.dao = new UserDAOImpl(dbc.getConnection());
 }
 public boolean findLogin(User user) throws Exception{
  boolean flag = false;
  try{
   flag = this.dao.findLogin(user);
  }catch(Exception e){
   throw e;
  }finally{
   this.dbc.close();
  }
  return flag;
 }
}

 

7、DAOFactory

 

 

package org.zxj.mvcdemo.factory;

import org.zxj.mvcdemo.dao.*;
import org.zxj.mvcdemo.dao.proxy.*;
public class DAOFactory{
 public static IUserDAO getIUserDAOInstance(){
  return new UserDAOProxy();
 }
}

 

8、LoginServlet 

package org.zxj.mvcdemo.servlet;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.zxj.mvcdemo.factory.*;
import org.zxj.mvcdemo.vo.*;

public class LoginServlet extends HttpServlet{
 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
  String path = "login.jsp";
  String userid = req.getParameter("userid");
  String userpass = req.getParameter("userpass");
  List<String> info = new ArrayList<String>();
  if(userid == null || "".equals(userid)){
   info.add("用户id不能为空");
  }
  if(userpass == null || "".equals(userpass)){
   info.add("密码不能为空");
  }
  if(info.size() == 0){
   User user = new User();
   user.setUSerid(userid);
   user.setPassword(userpass);
   try{
    if(DAOFactory.getIUserDAOInstance().findLogin(user)){
     info.add("用户登录成功,欢迎"+user.getName()+ "登录");
    }else{
     info.add("用户登录失败");
    } 
   }catch(Exception e){
    e.printStackTrace();
   }
  }
  req.setAttribute("info",info);
  req.getRequestDispatcher(path).forward(req,res);
  //RequestDispatcher rd = null;
  //rd = req.getRequestDispatcher(path);
  //rd.forward(req,res);
 
 }
 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
  doGet(req,res);
 }
} 

 

9、Login.jsp

 

 

 

<%@ page language="java" contentType="text/html"  pageEncoding="GBK" %>
<%@ page import="java.util.*"%>
<html>
 <head>
  <title>登录</title>
  <meta http-equiv="content-type" content="text/html;charset=gbk">
 </head>
 
 <body>
  <%
   request.setCharacterEncoding("GBK");
   List<String> info = (List<String>)request.getAttribute("info");
   if(info != null){
    Iterator<String> iter = info.iterator();
    while(iter.hasNext()){
     out.println(iter.next());
    }
   }
  %>
  <form action="LoginServlet" method="post">
   用户:<input type="text" name="userid" /> <br />
   密码:<input type="password" name="userpass" /><br />
   <input type="submit" value="登录" />
   <input type="reset" value="重置" />
 </body
</html>

最后

以上就是酷炫冬天为你收集整理的简单的servlet mvc实现的全部内容,希望文章能够帮你解决简单的servlet mvc实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部