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)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>
(2)addMessageSuccess.jsp:
<%@ 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>
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 doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
doGet(request,response);
}
}
路过的哥哥姐姐们,求打赏,博主连写5门考试科目,25篇博客好累,软工不熬夜要写不完了
用户注册:
1、jsp:
(1)register.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>网页左上角要写的标题</title>
</head>
<body>
<h3>用户注册</h3>
<form action="RegisterServlet" 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>
(2)welcome.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>网页左上角要写的标题</title>
</head>
<body>
<h3>注册成功<a href="register.jsp">再次注册</a></h3>
</body>
</html>
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 doPost(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" %>
<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>
(2)
loginSuccess.jsp:
<%@ 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>
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 doPost(HttpServletRequest request,HttpServletResponse)throws ServletException,IOException{
doGet(request,response);
}
}
整体看下来,其实MVC的题型大同小异,我们只需要根据题意,将几个必要的参数修改即可