MVC operation (30 branch )

Programming problem is to give you a table , How many? jsp Display diagram of page , Let you write according to the meaning of the title MVC code

among ,MVC Contains 3 part :

1, view (V)—— to write jsp page (HTML,CSS code )

2, Model (M)—— to write javabean.java( string manipulation , Database operation )

3, controller (C)—— to write servlet.java( Package pair HTTP Processing of requests )

These are the main types of questions : User registration , User login , guest book , Library management

If the topic is simple , Will only ask us to add MVC, If it's harder, we'll write it all MVC, This big question 30 branch , I guess it's a little difficult

What you can understand , If you can't understand it, just recite it ( It's all hand coded , Mistakes are inevitable , Tell me about the wrong code segment in the comment area )

guest book : 

1,jsp:

(1)addMessage.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %> <html> <head> <title> Title to be written in the upper left corner of the page </title> </head> <body>
<h3> guest book </h3> <form action="MessageServlet" method="post"> nickname :<input
type="text" name="nickname" /><br/><br/> theme :<input type="text" name="title"
/><br/><br/> content :<br/> <textarea rows="5" cols="25"
name="content"></textarea><br/><br/> <input type="submit" value=" Leaving a message. "/> <input
type="reset" value=" Reset "/> </form> </body> </html>
(2)addMessageSuccess.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %> <html> <head> <title> Title to be written in the upper left corner of the page </title> </head> <body>
<h3> Successfully added message !<a href="addMessage.jsp"> Leave a message again </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; } //...... The rest getter and setter Method omission , Write it yourself public
boolean addMessage(MessageBean message){ Integer id=message.getId(); String
title=message.getTitle(); //...... Two remaining omissions , Exam writing 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); } }
User registration :

1,jsp:

(1)register.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %> <html> <head> <title> Title to be written in the upper left corner of the page </title> </head> <body>
<h3> User registration </h3> <form action="RegisterServlet" method="post"> user name :<input
type="text" name="name" /><br/><br/> password :<input type="text" name="password"
/><br/><br/> <input type="submit" value=" Submit "/> <input type="reset" value=" Reset "/>
</form> </body> </html>
(2)welcome.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %> <html> <head> <title> Title to be written in the upper left corner of the page </title> </head> <body>
<h3> login was successful <a href="register.jsp"> Re register </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
doGet(HttpServletRequest request,HttpServletResponse)throws
ServletException,IOException{ doGet(request,response); } }
  User login :

1,jsp:

(1)login.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %> <html> <head> <title> Title to be written in the upper left corner of the page </title> </head> <body>
<h3> User login </h3> <form action="LoginServlet" method="post"> user name :<input type="text"
name="name" /><br/><br/> password :<input type="text" name="password" /><br/><br/>
<input type="submit" value=" Sign in "/> <input type="reset" value=" Reset "/> </form>
</body> </html>
(2)

loginSuccess.jsp:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" %> <html> <head> <title> Title to be written in the upper left corner of the page </title> </head> <body>
<h3> Login successful <a href="login.jsp"> Re register </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 doGet(HttpServletRequest
request,HttpServletResponse)throws ServletException,IOException{
doGet(request,response); } }
Look at it as a whole , actually MVC The types of questions are similar , We just need to , Modify several necessary parameters

 

 

Technology