赞
踩
听说考学生注册,那我就浅把用户注册改成学生注册
编程题就是给你一个表,几个jsp页面的显示图,让你根据题意写出MVC编码
其中,MVC包含了3部分:
1、视图(V)——编写jsp页面(HTML、CSS代码)
2、模型(M)——编写javabean.java(字符串处理、数据库操作)
3、控制器(C)——编写servlet.java(封装对HTTP请求的处理)
主要出的题型就这么几类:用户注册、用户登录、用户留言、图书管理
题目简单的话,只会要求我们补充MVC,难一点的话会让我们写全部的MVC,这次大题30分,估计难一点
大家能理解的理解,理解不了就背下来(都是手打的代码,难免出错,出错的代码段大家评论区告我)
(1)addStudent.jsp:
- <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- <html>
- <head>
- <title>网页左上角要写的标题</title>
- </head>
- <body>
- <h3>学生注册</h3>
- <form action="StudentServlet" method="post">
- 学生姓名:<input type="text" name="name" /><br/><br/>
- 学生年龄:<input type="text" name="age" /><br/><br/>
- <input type="submit" value="提交"/>
- <input type="reset" value="重置"/>
- </form>
- </body>
- </html>
- <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- <html>
- <head>
- <title>网页左上角要写的标题</title>
- </head>
- <body>
- 学生注册成功<br/>
- 学生姓名:${requestScope.name}<br/>
- 学生年龄:${requestScope.age}<br/>
- <a href="addStudent.jsp">再次注册</a>
- </body>
- </html>
- 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;
- }
- }
- 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");
- else
- response.sendRedirect("addStudent.jsp");
- }
- protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
- doGet(request,response);
- }
- }
(1)addMessage.jsp:
- <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- <html>
- <head>
- <title>网页左上角要写的标题</title>
- </head>
- <body>
- <h3>用户留言</h3>
- <form action="MessageServlet" method="post">
- 昵称:<input type="text" name="nickname" /><br/><br/>
- 主题:<input type="text" name="title" /><br/><br/>
- 内容:<br/>
- <textarea rows="5" cols="25" name="content"></textarea><br/><br/>
- <input type="submit" value="留言"/>
- <input type="reset" value="重置"/>
- </form>
- </body>
- </html>
- <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- <html>
- <head>
- <title>网页左上角要写的标题</title>
- </head>
- <body>
- <h3>添加留言成功!<a href="addMessage.jsp">再次留言</a></h3>
- </body>
- </html>
- 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(?,?,?)"
- PreparedStatement ps=conn.prepareStatement(sql);
- ps.setString(1,nickname);
- ps.setString(2,title);
- ps.setString(3,content);
- int result=ps.executeUpdate();
- if(result>0)
- return true;
- else
- return false;
- }catch(Exception e)
- {e.printStackTrace();}
- return false;
- }
- }
- 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");
- else
- response.sendRedirect("addMessage.jsp");
- }
- protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
- doGet(request,response);
- }
- }
(1)login.jsp:
- <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- <html>
- <head>
- <title>网页左上角要写的标题</title>
- </head>
- <body>
- <h3>用户登录</h3>
- <form action="LoginServlet" method="post">
- 用户名:<input type="text" name="name" /><br/><br/>
- 密码:<input type="text" name="password" /><br/><br/>
- <input type="submit" value="登录"/>
- <input type="reset" value="重置"/>
- </form>
- </body>
- </html>
- <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- <html>
- <head>
- <title>网页左上角要写的标题</title>
- </head>
- <body>
- <h3>登录成功<a href="login.jsp">再次注册</a></h3>
- </body>
- </html>
- 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;
- }
- }
- 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");
- else
- response.sendRedirect("login.jsp");
- }
- protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
- doGet(request,response);
- }
- }
整体看下来,其实MVC的题型大同小异,我们只需要根据题意,将几个必要的参数修改即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。