MVC操作(30分)
听说考学生注册,那我就浅把用户注册改成学生注册
编程题就是给你一个表,几个jsp页面的显示图,让你根据题意写出MVC编码
其中,MVC包含了3部分:
1、视图(V)——编写jsp页面(HTML、CSS代码)
2、模型(M)——编写javabean.java(字符串处理、数据库操作)
3、控制器(C)——编写servlet.java(封装对HTTP请求的处理)
主要出的题型就这么几类:用户注册、用户登录、用户留言、图书管理
题目简单的话,只会要求我们补充MVC,难一点的话会让我们写全部的MVC,这次大题30分,估计难一点
大家能理解的理解,理解不了就背下来(都是手打的代码,难免出错,出错的代码段大家评论区告我)
学生注册:
1、jsp:
(1)addStudent.jsp:
网页左上角要写的标题 学生注册
学生姓名:
学生年龄:
(2)addStudentSuccess.jsp:
网页左上角要写的标题 学生注册成功
学生姓名:${requestScope.name}
学生年龄:${requestScope.age}
再次注册
2、javabean:
import java.sql.*;public class StudentBean{private Integer id;private String name;private String age;public Integer getId(){return id;} public void setId(Integer id){this.id=id;} public String getName(){return name;} public void setName(String name){this.name=name;} public String getAge(){return age;} public void setAge(String age){this.age=age;} public boolean insortStudent(StudentBean student){ Integer id=student.getId(); String name=student.getName(); String age=student.getAge(); try{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/jdbc1"; Connection conn=DriverManager.getConnection(url,"root","123"); String sql="insert into student(name,age)values(?,?)" PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1,name); ps.setString(2,age); int result=ps.executeUpdate(); if(result>0) return true; else return false; }catch(Exception e) {e.printStackTrace();} return false; }}
3、servlet:
import java.io.IOException;import javax.servlet.*;import edu.tyut.bean.StudentServlet;@WebServlet("/StudentServlet")public class StudentServlet extends HttpServlet{protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{request.setCharacterEncoding("UTF-8");String name=request.getParameter("name");String age=request.getParameter("age");StudentBean student=new StudentBean();student.setName(name);student.setAge(age);boolean result=student.insortStudent(student);if(result)response.sendRedirect("addStudentSuccess.jsp");elseresponse.sendRedirect("addStudent.jsp");}protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{doGet(request,response);}}
用户留言:
1、jsp:
(1)addMessage.jsp:
网页左上角要写的标题 用户留言
昵称:
主题:
内容:
(2)addMessageSuccess.jsp:
网页左上角要写的标题 添加留言成功!再次留言
2、javabean:
import java.sql.*;public class MessageBean{private Integer id;private String nickname;private String title;private String content;public Integer getId(){return id;} public void setId(Integer id){this.id=id;} public String getNickname(){return nickname;} public void setNickname(String nickname){this.nickname=nickname;}//......剩下的getter和setter方法省略,考试自己写上 public boolean addMessage(MessageBean message){ Integer id=message.getId(); String title=message.getTitle(); //......剩下两个省略,考试写 try{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/jdbc1"; Connection conn=DriverManager.getConnection(url,"root","123"); String sql="insert into message(nickname,title,content)values(" />0) return true; else return false; }catch(Exception e) {e.printStackTrace();} return false; }}
3、servlet:
import java.io.IOException;import javax.servlet.*;import edu.tyut.bean.MessageBean;@WebServlet("/MessageServlet")public class MessageServlet extends HttpServlet{protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{request.setCharacterEncoding("UTF-8");String nickname=request.getParameter("nickname");String title=request.getParameter("title");String content=request.getParameter("content");MessageBean message=new MessageBean();message.setNickname(nickname);message.setTitle(title);message.setContent(content);boolean result=message.addMessage(message);if(result)response.sendRedirect("addMessageSuccess.jsp");elseresponse.sendRedirect("addMessage.jsp");}protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{doGet(request,response);}}
用户登录:
1、jsp:
(1)login.jsp:
网页左上角要写的标题 用户登录
用户名:
密码:
(2)
loginSuccess.jsp:
网页左上角要写的标题 登录成功再次注册
2、javabean:
import java.sql.*;public class UserBean{private Integer id;private String name;private String password;public Integer getId(){return id;} public void setId(Integer id){this.id=id;} public String getName(){return name;} public void setName(String name){this.name=name;} public String getPassword(){return password;} public void setPassword(String password){this.password=password;} public boolean loginUser(UserBean user){ Integer id=user.getId(); String name=user.getName(); String password=user.getPassword(); try{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/jdbc1"; Connection conn=DriverManager.getConnection(url,"root","123"); String sql="select*from user where name=? and password=?";//这个loginUser()方法我写的有些问题,先别参考 PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1,name); ps.setString(2,password); int result=ps.executeUpdate(); if(result>0) return true; else return false; }catch(Exception e) {e.printStackTrace();} return false; }}
3、servlet:
import java.io.IOException;import javax.servlet.*;import edu.tyut.bean.LoginServlet;@WebServlet("/LoginServlet")public class LoginServlet extends HttpServlet{protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{request.setCharacterEncoding("UTF-8");String name=request.getParameter("name");String password=request.getParameter("password");UserBean user=new UserBean();user.setName(name);user.setPassword(password);boolean result=user.loginUser(user);if(result)response.sendRedirect("loginSuccess.jsp");elseresponse.sendRedirect("login.jsp");}protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{doGet(request,response);}}
整体看下来,其实MVC的题型大同小异,我们只需要根据题意,将几个必要的参数修改即可