初级javaweb实例
一、开发项目原因
复习一下以前的知识。所以就有了想法开发一个小例子,简单的实现了权限、验证码、分页、MD5密码加密数据信息的增、删、改、查功能。小弟功力浅薄,望大神勿念。
二、功能实现部分
1、验证码实现
servlet部分:
1 package com.info.servlet;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.image.BufferedImage;
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import java.util.Random;
11
12 import javax.imageio.ImageIO;
13 import javax.servlet.ServletException;
14 import javax.servlet.ServletOutputStream;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import javax.servlet.http.HttpSession;
19
20 import net.sf.cglib.core.Constants;
21
22
23 public class ValidateCodeServlet extends HttpServlet {
24
25 /*
26 * 定义验证码图片属性
27 */
28 private static final long serialVersionUID = 1L;
29 public static final String CHECK_CODE = "checkCode";
30 private char mapTable[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
31 /**
32 * Constructor of the object.
33 */
34 public ValidateCodeServlet() {
35 super();
36 }
37
38 /**
39 * Destruction of the servlet. <br>
40 */
41 public void destroy() {
42 super.destroy(); // Just puts "destroy" string in log
43 // Put your code here
44 }
45
46 /**
47 * The doGet method of the servlet. <br>
48 *
49 * This method is called when a form has its tag value method equals to get.
50 *
51 * @param request the request send by the client to the server
52 * @param response the response send by the server to the client
53 * @throws ServletException if an error occurred
54 * @throws IOException if an error occurred
55 */
56 public void doGet(HttpServletRequest request, HttpServletResponse response)
57 throws ServletException, IOException {
58
59 response.setContentType("text/html");
60 this.doPost(request, response);
61 }
62
63 /**
64 * The doPost method of the servlet. <br>
65 *
66 * This method is called when a form has its tag value method equals to post.
67 *
68 * @param request the request send by the client to the server
69 * @param response the response send by the server to the client
70 * @throws ServletException if an error occurred
71 * @throws IOException if an error occurred
72 */
73 public void doPost(HttpServletRequest request, HttpServletResponse response)
74 throws ServletException, IOException {
75
76 response.setContentType("text/html");
77 try {
78 int width = 65, height = 24;
79 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
80 Graphics graphics = image.getGraphics();// 获取图形上下文
81 graphics.setColor(getRandColor(180, 250));// 设定背景色(最多色调255)
82 graphics.fillRect(0, 0, width, height);
83 graphics.setColor(Color.black);// 画边框
84 graphics.drawRect(0, 0, width - 1, height - 1);
85 String randCode = "";// 取随机产生的认证码
86 for (int i = 0; i < 4; ++i) {// 4代表4位验证码,如果要生成更多位的认证码,则加大数值
87 randCode += mapTable[(int) (mapTable.length * Math.random())];
88 }
89 graphics.setColor(Color.black);// 将认证码显示到图像中,如果要生成更多位的认证码,增加drawString语句
90 graphics.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
91 String str = randCode.substring(0, 1);
92 graphics.drawString(str, 8, 17);
93 str = randCode.substring(1, 2);
94 graphics.drawString(str, 20, 15);
95 str = randCode.substring(2, 3);
96 graphics.drawString(str, 35, 18);
97 str = randCode.substring(3, 4);
98 graphics.drawString(str, 45, 15);
99 Random rand = new Random();// 随机产生30个干扰点
100 for (int i = 0; i < 30; i++) {
101 int x = rand.nextInt(width);
102 int y = rand.nextInt(height);
103 graphics.drawOval(x, y, 1, 1);
104 }
105 graphics.dispose();// 释放图形上下文
106 request.getSession().setAttribute(CHECK_CODE, randCode);// 认证码保存在session中
107 ImageIO.write(image, "JPEG", response.getOutputStream());// 输出图像到页面
108 image.flush();
109 } catch (Exception e) {
110 e.printStackTrace();
111 }
112 }
113
114 /**
115 * Initialization of the servlet. <br>
116 *
117 * @throws ServletException if an error occurs
118 */
119 public void init() throws ServletException {
120
121 }
122
123 // 给定范围获得一个随机颜色
124 private Color getRandColor(int fc, int bc) {
125 Random random = new Random();
126 int r = fc + random.nextInt(bc - fc);
127 int g = fc + random.nextInt(bc - fc);
128 int b = fc + random.nextInt(bc - fc);
129 return new Color(r, g, b);
130 }
131 }
jsp页面部分:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>企业管理首页</title>
13 <meta http-equiv="pragma" content="no-cache">
14 <meta http-equiv="cache-control" content="no-cache">
15 <meta http-equiv="expires" content="0">
16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17 <meta http-equiv="description" content="This is my page">
18 <!--
19 <link rel="stylesheet" type="text/css" href="styles.css">
20 -->
21 <style type="text/css">
22 *{margin:0;padding:0;}
23 .main {margin:auto;width:651px;height:350px;margin-top:70px;}
24 .input { font-family:Arial, Helvetica, sans-serif; border:1px solid #999; font-size:12px; background:#dadedf;}
25 .table1_tr_td_1 {width:15px; background: url(image/ileft.gif); height:43px;}
26 .table1_tr_td_2 {width:620px; background:url(image/i_topbg2.gif) repeat-x; height:43px;}
27 .table1_tr_td_3 {width:16px; background:url(image/iright.gif); height:43px;}
28 .table1_tr_td_4 {width:15px; background:url(image/ileftbg.gif) repeat-y;}
29 .table1_tr_td_5 {width:620px;background:url(image/bg.gif) repeat-x; height:279px;}
30 .table1_tr_td_6 {width:155px; height:140px; background:url(image/logo.jpg) no-repeat;}
31 .table1_tr_td_7 {background:url(image/adminsyteam.gif) no-repeat; width:167px; height:30px;}
32 .table1_tr_td_8 {width:43px; height:14px; background:url(image/id.gif) no-repeat;}
33 .table1_tr_td_9 {width:43px; height:14px; background:url(image/pass.gif) no-repeat;}
34 .table1_tr_td_10 {width:16px; background: url(image/irightbg.gif) repeat-y;}
35 .table1_tr_td_11 {width:15px; background: url(image/i_bottom_left.gif); height:29px;}
36 .table1_tr_td_12 {width:620px;background: url(image/i_bottom_bg.gif) repeat-x; height:29px;}
37 .table1_tr_td_13 {width:16px; background: url(image/i_bottom_right.gif); height:29px;}
38 </style>
39 <script type="text/javascript">
40 /**窗体加载*/
41 window.onload = function() {
42 bringCode();
43 };
44
45 /**产生验证码*/
46 function bringCode() {
47 //var path = document.getElementById("hdfPath").value;
48 document.getElementById("imgCode").src = "<%=path%>/ValidateCodeServlet?random=" + Math.random();
49 }
50
51 </script>
52 </head>
53
54 <body>
55 <div class="main">
56 <table width="651px" cellpadding="0" cellspacing="0" border="0">
57 <tr>
58 <td class="table1_tr_td_1"></td>
59 <td class="table1_tr_td_2"></td>
60 <td class="table1_tr_td_3"></td>
61 </tr>
62 <tr>
63 <td class="table1_tr_td_4"></td>
64 <td class="table1_tr_td_5" align="center"><table width="369px" cellspacing="0" cellpadding="0" height="109px">
65 <tr>
66 <td class="table1_tr_td_6"></td>
67 <td style="width:214px;">
68 <form action="<%=path%>/UserInfoServlet" name="forms" method="post">
69 <table cellpadding="0" cellspaceing="0" border="0" align="center">
70 <tr>
71 <td colspan="2" align="center" class="table1_tr_td_7"></td>
72 </tr>
73 <tr style="font-size:12px;">
74 <td colspan="2" style="height:10px;">
75 <input type="radio" name="roleId" value="1"/> 管理员
76 <input type="radio" name="roleId" value="2"/> 普通用户
77 </td>
78 </tr>
79 <tr>
80 <td class="table1_tr_td_8"></td>
81 <td><input type="text" size="20" name="userName" id="userName" class="input"/></td>
82 </tr>
83 <tr>
84 <td colspan="2" style="height:10px;"></td>
85 </tr>
86 <tr style="margin-top:20px;">
87 <td class="table1_tr_td_9"></td>
88 <td><input type="password" size="20" name="passWord" id="passWord" class="input"/></td>
89 </tr>
90 <tr>
91 <td colspan="2" style="padding-top:5px;">
92 <label style="font-size:12px;">验证码:</label>
93 <input type="text" name = "txtCode" maxlength="4" style="height:20px;" size="5"/>
94 <img id="imgCode" style="margin-bottom:-5px;cursor:pointer;height:20px;" onclick="bringCode()" title="点击更换图片"/>
95 </td>
96 </tr>
97 <tr>
98 <td colspan="2" style="height:10px;"></td>
99 </tr>
100 <tr>
101 <td colspan="2"><div align="center">
102 <!--以图片做提交、重置按钮-->
103 <input type="image" src="image/b_login.gif" width="73px" height="21px" align=absMiddle border=0 name=RedImg onclick="document.forms.submit();"/>
104 <input type="reset" style="background:url(image/b_clean.gif);width:73px;height:21px;border:0;" value=" "/>
105 </td>
106 </tr>
107 </table>
108 </form>
109 </td>
110 </tr>
111 </table>
112 </td>
113 <td class="table1_tr_td_10"></td>
114 </tr>
115 <tr>
116 <td class="table1_tr_td_11"></td>
117 <td class="table1_tr_td_12"></td>
118 <td class="table1_tr_td_13"></td>
119 </tr>
120 </table>
121 </div>
122 <script type="text/javascript">
123 function reset() {
124 document.all.userName.innerText="";
125 document.all.passWord.innerText="";
126 }
127 </script>
128 </body>
129 </html>
2、加密实现部分
加密算法通过写在一个单独的加密类中,在所有需要用到加密的时候直接调用就可以。
1 package com.info.test; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 /* 6 * MD5 算法 7 */ 8 public class MD5 { 9 10 // 全局数组 11 private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", 12 "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 13 14 public MD5() { 15 } 16 17 // 返回形式为数字跟字符串 18 private static String byteToArrayString(byte bByte) { 19 int iRet = bByte; 20 if (iRet < 0) { 21 iRet += 256; 22 } 23 int iD1 = iRet / 16; 24 int iD2 = iRet % 16; 25 return strDigits[iD1] + strDigits[iD2]; 26 } 27 28 // 返回形式只为数字 29 private static String byteToNum(byte bByte) { 30 int iRet = bByte; 31 if (iRet < 0) { 32 iRet += 256; 33 } 34 return String.valueOf(iRet); 35 } 36 37 // 转换字节数组为16进制字串 38 private static String byteToString(byte[] bByte) { 39 StringBuffer sBuffer = new StringBuffer(); 40 for (int i = 0; i < bByte.length; i++) { 41 sBuffer.append(byteToArrayString(bByte[i])); 42 } 43 return sBuffer.toString(); 44 } 45 46 public static String GetMD5Code(String strObj) { 47 String resultString = null; 48 try { 49 resultString = new String(strObj); 50 MessageDigest md = MessageDigest.getInstance("MD5"); 51 // md.digest() 该函数返回值为存放哈希值结果的byte数组 52 resultString = byteToString(md.digest(strObj.getBytes())); 53 } catch (NoSuchAlgorithmException ex) { 54 ex.printStackTrace(); 55 } 56 return resultString; 57 } 58 59 public static void main(String[] args) { 60 MD5 getMD5 = new MD5(); 61 System.out.println(getMD5.GetMD5Code("1")); 62 } 63 }
3、主体项目部分
实体类层:com.info.util
数据层:com.info.manager
业务控制层:com.info.servlet
其他操作层:com.info.test
需要项目的童鞋可以联系QQ:850253631告诉我邮箱发给你。共勉!

浙公网安备 33010602011771号