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

MVC操作(30分)

编程题就是给你一个表,几个jsp页面的显示图,让你根据题意写出MVC编码

其中,MVC包含了3部分:

1、视图(V)——编写jsp页面(HTML、CSS代码)

2、模型(M)——编写javabean.java(字符串处理、数据库操作)

3、控制器(C)——编写servlet.java(封装对HTTP请求的处理)

主要出的题型就这么几类:用户注册、用户登录、用户留言、图书管理

题目简单的话,只会要求我们补充MVC,难一点的话会让我们写全部的MVC,这次大题30分,估计难一点

大家能理解的理解,理解不了就背下来(都是手打的代码,难免出错,出错的代码段大家评论区告我)

用户留言: 

TYUT太原理工大学2022javaweb编程大题_第1张图片

TYUT太原理工大学2022javaweb编程大题_第2张图片

TYUT太原理工大学2022javaweb编程大题_第3张图片TYUT太原理工大学2022javaweb编程大题_第4张图片

1、jsp:

(1)addMessage.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

    
        网页左上角要写的标题
    
    
        

用户留言

昵称:

主题:

内容:


(2)addMessageSuccess.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

    
        网页左上角要写的标题
    
    
        

添加留言成功!再次留言

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(?,?,?)"
 		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 turn;
 		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");
		else
			response.sendRedirect("addMessage.jsp");
	}
	protected void doGet(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
		doGet(request,response);
	}
}

用户注册:

1、jsp:

(1)register.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

    
        网页左上角要写的标题
    
    
        

用户注册

用户名:

密码:

(2)welcome.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

    
        网页左上角要写的标题
    
    
        

注册成功再次注册

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 insortUser(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="insert into user(name,password)values(?,?)"
 		PreparedStatement ps=conn.prepareStatement(sql);
 		ps.setString(1,name);
 		ps.setString(2,password);
 		int result=ps.executeUpdate();
 		if(result>0)
 			return turn;
 		else
 			return false;
 		}catch(Exception e)
 			{e.printStackTrace();}
 		return false;		
 	}
 }
}

3、servlet:

import java.io.IOException;
import javax.servlet.*;
import edu.tyut.bean.RegisterServlet;
@WebServlet("/RegisterServlet")
public class RegisterServlet 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.register(user);
		if(result)
			response.sendRedirect("welcome.jsp");
		else
			response.sendRedirect("register.jsp");
	}
	protected void doGet(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
		doGet(request,response);
	}
}

 用户登录:

1、jsp:

(1)login.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

    
        网页左上角要写的标题
    
    
        

用户登录

用户名:

密码:

(2)

loginSuccess.jsp:

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>

    
        网页左上角要写的标题
    
    
        

登录成功再次注册

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="insert into user(name,password)values(?,?)"
 		PreparedStatement ps=conn.prepareStatement(sql);
 		ps.setString(1,name);
 		ps.setString(2,password);
 		int result=ps.executeUpdate();
 		if(result>0)
 			return turn;
 		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.register(user);
		if(result)
			response.sendRedirect("loginSuccess.jsp");
		else
			response.sendRedirect("login.jsp");
	}
	protected void doGet(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
		doGet(request,response);
	}
}

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

 

 

你可能感兴趣的:(javaweb,学习,mvc)