滨州学院
课程设计(学年论文)
题目:NCRE考试报名系统设计与实现
系 院
专 业
班 级
姓 名
学 号
指导教师
职 称
年月 日
滨州学院课程设计(学年论文)任务书
指导教师签字:
系(院)负责人签字:
滨州学院课程设计(学年论文)成绩评定表
系(院):
一、课程设计的目的及要求
加深对JSP动态网页开发技术中的基本知识的理解,通过对所选项目的分析、程序算法的设计、运行与调试过程的分析,掌握动态网页基础知识、Tomcat服务器管理知识、JSP基础、JSP指令和脚本元素、JDBC、JSP处理客户端请求、会话和访问控制等,以及上机操作的各种技巧,培养逻辑思维能力、团队合作精神、创新能力、动手操作能力等,为本专业后续课程的学习打下一定的基础。
二、系统开发环境及运行环境
操作系统:windows xp
数据库系统:MySQL
开发工具:MyEclipse
三、设计正文
此课程设计的网址是http://localhost:8080/NCRE
进入之后是网址的首页,存在一个进入链接
点击链接后进入主页,主页存在课程设计的两方面,分别是管理员和用户
在管理员中,管理员有登陆,修改密码,显示、修改用户信息的能力
管理员的部分代码
<一> 用户名和密码不能为空的验证
<script type="text/javascript">
function checkzhi(){
if(form1.text1.value==null||form1.text1.value==""){
alert("请输入账号");
return false;
}
if(form1.pw.value==null||form1.pw.value==""){
alert("请输入密码");
return false;
}
if(form1.text2.value==null||form1.text2.value==""){
alert("请输入验证码");
return false;
}
}
</script>
<二> 验证码验证代码
public class CheckServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String requestCaptcha = request.getParameter("captcha");
String sessionCaptcha = (String) session.getAttribute("captcha");
if (sessionCaptcha != null && sessionCaptcha.equals(requestCaptcha)) {
session.removeAttribute("captcha");
request.getRequestDispatcher("/jsp/gl.jsp").forward(request, response);
} else {
request.setAttribute("message", "验证码输入错误");
request.getRequestDispatcher("/jsp/gly.jsp").forward(request, response);
}
}
}
public class EncodingFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
}
<三> 验证码绘制
<%!
Color getRandColor(int fc,int bc) {
Random random = new Random();
if(fc > 255) {
fc = 255;
}
if(bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
%>
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String sRand = "";
for (int i = 0;i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
session.setAttribute("captcha", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>
<四> 数据库链接代码
public class Db {
private final String QD = "com.mysql.jdbc.Driver";
private final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
private final String USER = "root";
private final String PWD = "zxy910121";
private Connection con = null; //数据库连接
private PreparedStatement pre = null; //预编译指令集
private ResultSet result = null; //结果集
public void connect(){
try{
Class.forName(QD);
con = DriverManager.getConnection(URL, USER, PWD);
}catch(Exception ex){
ex.printStackTrace();
}
}
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public PreparedStatement getPre() {
return pre;
}
public void setPre(PreparedStatement pre) {
this.pre = pre;
}
public ResultSet getResult() {
return result;
}
public void setResult(ResultSet result) {
this.result = result;
}
}
<五> 数据修改代码
try{
Db db = new Db();
db.connect();
String sql = "update yonghu set shenfenzhenghao=?,ybukao=?,yminzu=?,ywenhua=?,yzhiye=?,kemu=?,yphone=?,yaddress=? where yname = ?";
db.setPre(db.getCon().prepareStatement(sql));
db.getPre().setString(1,shenfen);
db.getPre().setString(2,bukao);
db.getPre().setString(3,minzu);
db.getPre().setString(4,wenhua);
db.getPre().setString(5,zhiye);
db.getPre().setString(6,kemu);
db.getPre().setString(7,phone);
db.getPre().setString(8,address);
db.getPre().setString(9,yname);
db.getPre().executeUpdate();
db.getCon().close();
request.getRequestDispatcher("login.jsp").forward(request, response);
}catch(Exception ex){
ex.printStackTrace();
}
<六> 数据库添加代码
try{
Db db = new Db();
db.connect();
String sql = "select * from yonghu where yname = ? ";
db.setPre(db.getCon().prepareStatement(sql));
db.getPre().setString(1,yname);
db.setResult(db.getPre().executeQuery());
if(db.getResult().next()){
request.setAttribute("message", "用户名已经存在");
request.getRequestDispatcher("yhzc.jsp").forward(request, response);
}else{
sql = "insert into yonghu(yname,ypass,ysex,yschool,yzhuanye,ybukao) values(?,?,?,?,?,?)";
db.setPre(db.getCon().prepareStatement(sql));
db.getPre().setString(1,yname);
db.getPre().setString(2,ypass);
db.getPre().setString(3,ysex);
db.getPre().setString(4,yschool);
db.getPre().setString(5,yzhuanye);
db.getPre().setString(6,ybukao);
db.getPre().executeUpdate();
request.getRequestDispatcher("zhuye.jsp").forward(request, response);
}
if(db.getResult()!=null){
db.getResult().close();
db.setResult(null);
}
if(db.getPre()!=null){
db.getPre().close();
db.setPre(null);
}
if(db.getCon()!=null){
db.getCon().close();
db.setCon(null);
}
}catch(Exception ex){
ex.printStackTrace();
}
四、程序调试过程中出现的问题
1、数据库都连接好了,但是验证用户名和其他有关数据库的东西时候总是出错
错误原因: final String URL = "jdbc:mysql://localhost:3306/students system?useUnicode=true&characterEncoding=GB2312";
这句话里面讲路径写成SQLServer的路径
final String USER = "root";
这里是用户名,写成了连接名
final String PWD = "123";
2、连接数据库的时候点击登录按钮没有反应
错误原因:
final String sql="select * from USERInfo Where uid =? and pwd =?";与之有关联的uid和pwd在MySQL中写的是用户名和密码
五 总结
通过实验,我学到了很多的jsp的知识,同时使自己对于面向对象的思想更加的理解,虽然期间出现了很多的问题,但是通过团队的努力,都解决了,增加了自己的知识总结,并且使自己的记忆更加的深刻。