javaweb的小项目总结(一)
花了一个星期左右的时间做了一个小的增删改查系统期间学到很多
在此做个总结:
项目采用前端框架H-UI+数据库jdbc+Mysql
- 验证码生成:
效果如下:
这个实现起来比较简单直接贴代码:(创建一个图像然后写到网页中去)
captche.java
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//创建一个内存图像BufferedImage
BufferedImage bfimage = new BufferedImage(120,25,BufferedImage.TYPE_INT_RGB);
//得到Graphics
Graphics gp = bfimage.getGraphics();
//设置画笔颜色
gp.setColor(Color.BLUE);
//画边框
gp.drawRect(0, 0,Width, Height);
//设置背景色
gp.setColor(Color.gray);
gp.fillRect(1, 1, Width-2, Height-2);
//画随机干扰线
gp.setColor(Color.YELLOW);
Random rd = new Random();
for(int i=0;i<9;i++){
gp.drawLine(rd.nextInt(Width),rd.nextInt(Height),rd.nextInt(Width),rd.nextInt(Height));
}
//写入随机字母或数字
gp.setColor(Color.RED);
gp.setFont(new Font("宋体",Font.BOLD,20));
int x = 5;
String captche = "";
for(int i=0;i<4;i++){
int se = rd.nextInt(2);
switch(se){
case 0:
String ch =""+(char)((rd.nextInt(26))+'A');
gp.drawString(ch+" ",x,20);
captche+=ch;
break;
case 1:
String num = ""+(rd.nextInt(9));
gp.drawString(num+" ", x,20);
captche+=num;
break;
}
x+=30;
}
request.getSession().setAttribute("captche",captche);
//写到页面
ImageIO.write(bfimage, "jpeg",response.getOutputStream());
}
2.换一张的实现:
实现看不清换一张(主要通过js在点击这个的时候再次发送一次请求)
<img id = "captche" src="captche.html">...</img>
js:
//刷新验证码
function change(){
var imgobj = document.getElementById("captche");
imgobj.src = "captche.html?"+newDate().getTime();
}
至此首页的验证码实现完成
3.数据实现
要开始写登录校验之前生成一种用户表
然后开始写数据库对应的操作(具体操作下次写这次先写流程和简单的实现)
4.登录校验
校验用户名,密码,验证码
其中在生成验证码的时候吧验证码写入了session中
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int maxtime = 300;
//设置编码格式
request.setCharacterEncoding("utf-8");
//设置编码格式统一
response.setContentType("text/html;charset=UTF-8");
//得到用户名
String username = request.getParameter("username");
//得到密码
String password = request.getParameter("password");
//得到验证码
String captche = request.getParameter("captche");
String check = request.getParameter("check");
//得到session
HttpSession session = request.getSession();
//取得当前验证码
String rdcaptche = (String) session.getAttribute("captche");
PrintWriter out = response.getWriter();
JSONObject jsonobj = new JSONObject();
if(!captche.equalsIgnoreCase(rdcaptche)){
session.setAttribute("islogin",false);
jsonobj.put("result","验证码错误");
jsonobj.put("login",false);
}else if(Check(username,password)==0){
session.setAttribute("islogin",false);
jsonobj.put("result","用户名或密码错误");
jsonobj.put("login",false);
}else if(Check(username,password)==2){
session.setAttribute("islogin",false);
jsonobj.put("result","账户被锁定请联系超级管理员");
jsonobj.put("login",false);
}else {
session.setAttribute("islogin",true);
session.setAttribute("userid",username);//session存值
if(check.equals("true")){
maxtime = 3600;
}
jsonobj.put("result","登录成功");
jsonobj.put("login",true);
}
session.setMaxInactiveInterval(maxtime);
out.print(jsonobj.toString());
out.flush();
out.close();
}
//校验账号
private int Check(String username, String password) {
UserService us = new UserService();
User user = us.findUser(username);
if(user == null){
return 0;
}
if(user.getUpassword().equals(password)){
if(user.getIsallow()==0){
return 2;
}
return 1;
}
return 0;
}
5.增删改查操作的实现
对应数据库的操作(后面会写)
如下:
6.Filter的实现过滤除login.html和captche.html外的所有页面
public class LoginFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain ) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
String path = req.getRequestURI();
//
if(path.indexOf("/login.html")>=0||!path.contains(".html")||path.indexOf("/captche.html")>=0){
chain.doFilter(request, response);
return;
}
HttpSession session = req.getSession();
if(session!=null){
Boolean islogin = (Boolean)session.getAttribute("islogin");
String userid = (String) session.getAttribute("userid");
if((islogin!=null && islogin)||(userid!=null&&userid.equals("true"))){
chain.doFilter(request, response);
}else {
res.sendRedirect("/WebDemo/login.html");
}
}else {
res.sendRedirect("/WebDemo/login.html");
}
}
对应数据库的操作会在后面的博文中继续写出来包括数据的排序和展示算是对自己的总结,项目不难 希望自己以后也要多多学习。