inotgo.com


Tool version compatibility problem
MVC Is a layered design pattern , This chapter starts with Servlet and JSP The limitations of , Slowly introduce MVC Concept of .
Correct

Step 1 : Just use Servlet My weaknesses   
Step 2 : Just use JSP My weaknesses   
Step 3 : combination Servlet and JSP   
Step 4 : MVC Design patterns   

Step 1 :

Just use Servlet My weaknesses

edit top fracture
stay Servlet In the chapter of , Used Edit Hero of Servlet
According to the submitted by the browser id, Through HeroDAO Find the corresponding Hero, Then in Servlet Organize html Show .

You can see this Servlet Not only to prepare the data , And prepare html. Especially prepare html, Very poor readability , Maintenance is also troublesome
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Hero; import dao.HeroDAO; public class HeroEditServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); Hero hero = new HeroDAO().get(id); StringBuffer format = new StringBuffer(); response.setContentType("text/html; charset=UTF-8"); format.append("<!DOCTYPE html>"); format.append("<form action='updateHero' method='post'>"); format.append(" name : <input type='text' name='name' value='%s' > <br>"); format.append(" Blood volume : <input type='text' name='hp' value='%f' > <br>"); format.append(" hurt : <input type='text' name='damage' value='%d' > <br>"); format.append("<input type='hidden' name='id' value='%d'>"); format.append("<input type='submit' value=' to update '>"); format.append("</form>"); String html = String.format(format.toString(), hero.getName(), hero.getHp(), hero.getDamage(), hero.getId()); response.getWriter().write(html); } }
Because in Servlet Write html There is such a short board , So just go straight to JSP Development editor in Hero This function

Then you will find , Although write html Convenient , But write java The code is not as good as Servlet So convenient in
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,bean.*,java.sql.*"%> <% int id = Integer.parseInt(request.getParameter("id")); Hero hero = null; try { Class.forName("com.mysql.jdbc.Driver"); Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8", "root", "admin"); Statement s = c.createStatement(); String sql = "select * from hero where id = " + id; ResultSet rs = s.executeQuery(sql); if (rs.next()) { hero = new Hero(); String name = rs.getString(2); float hp = rs.getFloat("hp"); int damage = rs.getInt(4); hero.name = name; hero.hp = hp; hero.damage = damage; hero.id = id; } s.close(); c.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } %> <form action='updateHero' method='post'> name : <input type='text' name='name' value='<%=hero.getName()%>'> <br> Blood volume :<input type='text' name='hp' value='<%=hero.getHp()%>'> <br> hurt : <input type='text' name='damage' value='<%=hero.getDamage()%>'> <br> <input type='hidden' name='id' value='<%=hero.getId()%>'> <input type='submit' value=' to update '> </form>
since Servlet and JSP Each has its own advantages and disadvantages , So why not combine them to develop strengths and avoid weaknesses ?
HeroEditServlet: Only used to query from the database Hero Object , Then jump to JSP page

request.setAttribute("hero", hero);

stay request Set... In hero

request.getRequestDispatcher("editHero.jsp").forward(request, response);

The server jumps to editHero.jsp, Because the server jumps , All belong to Same request , So you can editHero.jsp Through request take out

editHero.jsp: Do not query the database , Get directly from HeroEditServlet It came from Hero Object , Through EL The expression puts request In hero Show
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Hero; import dao.HeroDAO; public class HeroEditServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); Hero hero = new HeroDAO().get(id); request.setAttribute("hero", hero); request.getRequestDispatcher("editHero.jsp").forward(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,bean.*,java.sql.*"%> <form action='updateHero' method='post'> name : <input type='text' name='name' value='${hero.name}'> <br> Blood volume :<input type='text' name='hp' value='${hero.hp}'> <br> hurt : <input type='text' name='damage' value='${hero.damage}'> <br> <input type='hidden' name='id' value='${hero.id}'> <input type='submit' value=' to update '> </form>
The above examples are combined with Serlvet and JSP Display data , It's a kind of MVC My thoughts .

M On behalf of Model ( Model
V On behalf of view ( View
C On behalf of controller ( controller)

What is the model ? Models are data , namely dao,bean

What is the view ? It's a web page , JSP, Used to show the data in the model

What is the controller ? The controller is used to put different data , Display on different views . In this example ,Servlet Is to act as a controller , hold Hero Object , Displayed in JSP Go ahead .

The function of the controller is to put different data (Model), Displayed in different views (View) Go ahead .
MVC Design patterns


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2020-10-04 hello MVC here editHero.jsp Is it just for the use of el The expression takes value ?
CJJJ

about J2EE-MVC-Hello MVC Your questions
use HTML Is there any way to get hero The value of ?




3 One answer

VOID1
Answer time :2021-04-07
get hero Still use jsp of request bar ,servlet Forwarded request It's the same

492554146
Answer time :2020-10-25
@ Broken strings and falling flowers Express doubt , Excuse me? html Where did it come from request???

Broken strings and falling flowers
Answer time :2020-10-23
brother html Not capable of dynamic web pages , Unless you use JavaScript, You turn the data into JSON All right , Then use JavaScript Traverse , Good sand sculpture
var data = <%=JSON.toJSONString(request.getAttribute(" name "))%>;



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-07-12 Unable to display data
lin1

about J2EE-MVC-Hello MVC Your questions



visit listHero The display is as shown in the figure , Database connection succeeded , There are also data
 Loading
 
               
 
               


3 One answer

ecolour
Answer time :2020-10-11
Brothers , Cannot directly access xxx.JSP, You must open the page with a map .web.xml The mapping name and address configured inside can be accessed with whatever , Just like php The framework routing is similar to ,JSP The file is only used for processing data display .

newbeeee
Answer time :2020-09-28
You don't have setAttribute

sparksun007
Answer time :2020-09-21
Post the code , Otherwise, how to find the wrong one ...



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2020-02-02 I don't understand the process
2019-07-01 Landlord , The last example is whether there is a submission ID My page didn't come up ?edithero.jsp It is used to display the results. It will cover the submission ID What about the page ?
2019-03-06 this updateHero refer to ?


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 3 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about J2EE-MVC-Hello MVC Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot