JavaWeb课程设计-学⽣信息管理系统(Jsp+Servlet,java⼊门代码maxWait=3000
然后编写数据库连接⼯具类:
/**
JDBC⼯具类 使⽤Durid连接池
*/
public class JDBCUtils {
private static DataSource ds ;
static {
try {
//1.加载配置⽂件
Properties pro = new Properties();
/
/使⽤ClassLoader加载配置⽂件,获取字节输⼊流
InputStream is = ClassLoader().getResourceAsStream(“druid.properties”);
pro.load(is);
//2.初始化连接池对象
ds = ateDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/
**
获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
javaservlet和jsp的比较/**
获取连接Connection对象
*/
public static Connection getConnection() throws SQLException {
Connection();
}
}
这⾥我们使⽤JdbcTemplate来进⾏数据连接操作数据库:
private JdbcTemplate template = new DataSource());
2. 实现持久层(Dao)
编写持久层接⼝:
/**
⽤户操作的DAO
*/
public interface UserDao {
}
实现持久层接⼝:
public class UserDaoImpl implements UserDao {
private JdbcTemplate template = new DataSource());
}
3. 实现业务层(Service)
编写业务层接⼝:
/**
⽤户管理的业务接⼝
*/
public interface UserService {
}
实现业务层接⼝:
public class UserServiceImpl implements UserService {
private UserDao dao = new UserDaoImpl();
}
4.实现表现层功能
编写表现层:
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
Code
*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);
}
}
5.由于表现层Servlet太多,我们可以做简单的提取
编写BaseServlet类,然后由其他servlet继承
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {
// 获取请求标识
String methodName = Parameter(“method”);
// 获取指定类的字节码对象
Class<? extends BaseServlet> clazz = Class();//这⾥的this指的是继承BaseServlet对象
// 通过类的字节码对象获取⽅法的字节码对象
Method method = Method(methodName, HttpServletRequest.class, HttpServletResponse.class);
// 让⽅法执⾏
method.invoke(this, request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.编写对应的前端页⾯:以user_login.jsp为例
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
管理员登录
⽤户名:
密码:
验证码:
×
${login_msg}
运⾏截图:
测试登录功能,发现中⽂乱码问题(直接继承HttpServlet不会出现,继承BaseServlet会出现)
6.编写过滤器解决中⽂乱码问题
@WebFilter("/*")
public class CharchaterFilter implements Filter {
protected String encoding;
@Override
public void destroy() {
// TODO ⾃动⽣成的⽅法存根
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO ⾃动⽣成的⽅法存根
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;
String Method();
if(method.equalsIgnoreCase(“post”)){
request.setCharacterEncoding(“utf-8”);
}
response.setContentType(“text/html;charset=utf-8”);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO ⾃动⽣成的⽅法存根
}
}
7.编写列表页⾯,并在后端代码上实现响应的功能
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
编号 姓名 性别 年龄 班级 电话 邮箱
<c:forEach items="${pb.list}" var=“student” varStatus=“s”>
${student.id} ${student.name} ${der} ${student.age} ${student.classno} ${student.phone} ${ail}  </c:forEach>
<c:if test="${pb.currentPage == 1}">
</c:if>
<c:if test="${pb.currentPage != 1}">
</c:if>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。