当前位置:   article > 正文

TYUT太原理工大学2022javaweb编程大题_太原理工大学javaweb

太原理工大学javaweb

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:

  1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  2. <html>
  3. <head>
  4. <title>网页左上角要写的标题</title>
  5. </head>
  6. <body>
  7. <h3>学生注册</h3>
  8. <form action="StudentServlet" method="post">
  9. 学生姓名:<input type="text" name="name" /><br/><br/>
  10. 学生年龄:<input type="text" name="age" /><br/><br/>
  11. <input type="submit" value="提交"/>
  12. <input type="reset" value="重置"/>
  13. </form>
  14. </body>
  15. </html>

(2)addStudentSuccess.jsp:

  1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  2. <html>
  3. <head>
  4. <title>网页左上角要写的标题</title>
  5. </head>
  6. <body>
  7. 学生注册成功<br/>
  8. 学生姓名:${requestScope.name}<br/>
  9. 学生年龄:${requestScope.age}<br/>
  10. <a href="addStudent.jsp">再次注册</a>
  11. </body>
  12. </html>

2、javabean:

  1. import java.sql.*;
  2. public class StudentBean{
  3. private Integer id;
  4. private String name;
  5. private String age;
  6. public Integer getId(){
  7. return id;
  8. }
  9. public void setId(Integer id){
  10. this.id=id;
  11. }
  12. public String getName(){
  13. return name;
  14. }
  15. public void setName(String name){
  16. this.name=name;
  17. }
  18. public String getAge(){
  19. return age;
  20. }
  21. public void setAge(String age){
  22. this.age=age;
  23. }
  24. public boolean insortStudent(StudentBean student){
  25. Integer id=student.getId();
  26. String name=student.getName();
  27. String age=student.getAge();
  28. try{
  29. Class.forName("com.mysql.jdbc.Driver");
  30. String url="jdbc:mysql://localhost:3306/jdbc1";
  31. Connection conn=DriverManager.getConnection(url,"root","123");
  32. String sql="insert into student(name,age)values(?,?)"
  33. PreparedStatement ps=conn.prepareStatement(sql);
  34. ps.setString(1,name);
  35. ps.setString(2,age);
  36. int result=ps.executeUpdate();
  37. if(result>0)
  38. return true;
  39. else
  40. return false;
  41. }catch(Exception e)
  42. {e.printStackTrace();}
  43. return false;
  44. }
  45. }

3、servlet:

  1. import java.io.IOException;
  2. import javax.servlet.*;
  3. import edu.tyut.bean.StudentServlet;
  4. @WebServlet("/StudentServlet")
  5. public class StudentServlet extends HttpServlet{
  6. protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
  7. request.setCharacterEncoding("UTF-8");
  8. String name=request.getParameter("name");
  9. String age=request.getParameter("age");
  10. StudentBean student=new StudentBean();
  11. student.setName(name);
  12. student.setAge(age);
  13. boolean result=student.insortStudent(student);
  14. if(result)
  15. response.sendRedirect("addStudentSuccess.jsp");
  16. else
  17. response.sendRedirect("addStudent.jsp");
  18. }
  19. protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
  20. doGet(request,response);
  21. }
  22. }

用户留言: 

a112ccfaa9f74056955ec27cd26c4b7b.png

a47e7ed7e0b04c60b6e70d3d49c01d3d.png

354a1d9f948d413f8204e677f736f687.png3f142c69ae7242c79599db99ef55098f.png

1、jsp:

(1)addMessage.jsp:

  1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  2. <html>
  3. <head>
  4. <title>网页左上角要写的标题</title>
  5. </head>
  6. <body>
  7. <h3>用户留言</h3>
  8. <form action="MessageServlet" method="post">
  9. 昵称:<input type="text" name="nickname" /><br/><br/>
  10. 主题:<input type="text" name="title" /><br/><br/>
  11. 内容:<br/>
  12. <textarea rows="5" cols="25" name="content"></textarea><br/><br/>
  13. <input type="submit" value="留言"/>
  14. <input type="reset" value="重置"/>
  15. </form>
  16. </body>
  17. </html>

(2)addMessageSuccess.jsp:

  1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  2. <html>
  3. <head>
  4. <title>网页左上角要写的标题</title>
  5. </head>
  6. <body>
  7. <h3>添加留言成功!<a href="addMessage.jsp">再次留言</a></h3>
  8. </body>
  9. </html>

2、javabean:

  1. import java.sql.*;
  2. public class MessageBean{
  3. private Integer id;
  4. private String nickname;
  5. private String title;
  6. private String content;
  7. public Integer getId(){
  8. return id;
  9. }
  10. public void setId(Integer id){
  11. this.id=id;
  12. }
  13. public String getNickname(){
  14. return nickname;
  15. }
  16. public void setNickname(String nickname){
  17. this.nickname=nickname;
  18. }
  19. //......剩下的getter和setter方法省略,考试自己写上
  20. public boolean addMessage(MessageBean message){
  21. Integer id=message.getId();
  22. String title=message.getTitle();
  23. //......剩下两个省略,考试写
  24. try{
  25. Class.forName("com.mysql.jdbc.Driver");
  26. String url="jdbc:mysql://localhost:3306/jdbc1";
  27. Connection conn=DriverManager.getConnection(url,"root","123");
  28. String sql="insert into message(nickname,title,content)values(?,?,?)"
  29. PreparedStatement ps=conn.prepareStatement(sql);
  30. ps.setString(1,nickname);
  31. ps.setString(2,title);
  32. ps.setString(3,content);
  33. int result=ps.executeUpdate();
  34. if(result>0)
  35. return true;
  36. else
  37. return false;
  38. }catch(Exception e)
  39. {e.printStackTrace();}
  40. return false;
  41. }
  42. }

3、servlet:

  1. import java.io.IOException;
  2. import javax.servlet.*;
  3. import edu.tyut.bean.MessageBean;
  4. @WebServlet("/MessageServlet")
  5. public class MessageServlet extends HttpServlet{
  6. protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
  7. request.setCharacterEncoding("UTF-8");
  8. String nickname=request.getParameter("nickname");
  9. String title=request.getParameter("title");
  10. String content=request.getParameter("content");
  11. MessageBean message=new MessageBean();
  12. message.setNickname(nickname);
  13. message.setTitle(title);
  14. message.setContent(content);
  15. boolean result=message.addMessage(message);
  16. if(result)
  17. response.sendRedirect("addMessageSuccess.jsp");
  18. else
  19. response.sendRedirect("addMessage.jsp");
  20. }
  21. protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
  22. doGet(request,response);
  23. }
  24. }

 用户登录:

1、jsp:

(1)login.jsp:

  1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  2. <html>
  3. <head>
  4. <title>网页左上角要写的标题</title>
  5. </head>
  6. <body>
  7. <h3>用户登录</h3>
  8. <form action="LoginServlet" method="post">
  9. 用户名:<input type="text" name="name" /><br/><br/>
  10. 密码:<input type="text" name="password" /><br/><br/>
  11. <input type="submit" value="登录"/>
  12. <input type="reset" value="重置"/>
  13. </form>
  14. </body>
  15. </html>

(2)

loginSuccess.jsp:

  1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
  2. <html>
  3. <head>
  4. <title>网页左上角要写的标题</title>
  5. </head>
  6. <body>
  7. <h3>登录成功<a href="login.jsp">再次注册</a></h3>
  8. </body>
  9. </html>

2、javabean:

  1. import java.sql.*;
  2. public class UserBean{
  3. private Integer id;
  4. private String name;
  5. private String password;
  6. public Integer getId(){
  7. return id;
  8. }
  9. public void setId(Integer id){
  10. this.id=id;
  11. }
  12. public String getName(){
  13. return name;
  14. }
  15. public void setName(String name){
  16. this.name=name;
  17. }
  18. public String getPassword(){
  19. return password;
  20. }
  21. public void setPassword(String password){
  22. this.password=password;
  23. }
  24. public boolean loginUser(UserBean user){
  25. Integer id=user.getId();
  26. String name=user.getName();
  27. String password=user.getPassword();
  28. try{
  29. Class.forName("com.mysql.jdbc.Driver");
  30. String url="jdbc:mysql://localhost:3306/jdbc1";
  31. Connection conn=DriverManager.getConnection(url,"root","123");
  32. String sql="select*from user where name=? and password=?";
  33. //这个loginUser()方法我写的有些问题,先别参考
  34. PreparedStatement ps=conn.prepareStatement(sql);
  35. ps.setString(1,name);
  36. ps.setString(2,password);
  37. int result=ps.executeUpdate();
  38. if(result>0)
  39. return true;
  40. else
  41. return false;
  42. }catch(Exception e)
  43. {e.printStackTrace();}
  44. return false;
  45. }
  46. }

3、servlet:

  1. import java.io.IOException;
  2. import javax.servlet.*;
  3. import edu.tyut.bean.LoginServlet;
  4. @WebServlet("/LoginServlet")
  5. public class LoginServlet extends HttpServlet{
  6. protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
  7. request.setCharacterEncoding("UTF-8");
  8. String name=request.getParameter("name");
  9. String password=request.getParameter("password");
  10. UserBean user=new UserBean();
  11. user.setName(name);
  12. user.setPassword(password);
  13. boolean result=user.loginUser(user);
  14. if(result)
  15. response.sendRedirect("loginSuccess.jsp");
  16. else
  17. response.sendRedirect("login.jsp");
  18. }
  19. protected void doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
  20. doGet(request,response);
  21. }
  22. }

整体看下来,其实MVC的题型大同小异,我们只需要根据题意,将几个必要的参数修改即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/661537
推荐阅读
相关标签
  

闽ICP备14008679号