Javaweb Development---MVC case query

Source: Internet
Author: User

Now javaweb to develop the first stage of learning to step into the practical stage, here to write down their own projects, but also easy to accumulate experience.

is the processing process:

The MySQL database table looks like this:

1. Create a new test.jsp in Web project webcontent

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>

<a href= "Listallstudentsservlet" >list all students</a>

</body>

2 Create a new Listallstudentservlet.java in JAVA-SRC and configure it

Package COM.JAVAWEB.MVC;

Import java.io.IOException;
Import Java.util.Arrays;
Import java.util.List;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;


public class Listallstudentsservlet extends HttpServlet {
Private static final long serialversionuid = 1L;


protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

Studentdao Studentdao = new Studentdao ();
list<student> students = Studentdao.getall ();

Request.setattribute ("Students", students);
Request.getrequestdispatcher ("/students.jsp"). Forward (request, response);


}

}

3. Continue to create Student.java

Package COM.JAVAWEB.MVC;

public class Student {
Private Integer ID;
Private String Studentname;
Private String location;
Private String telephone;

Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String Getstudentname () {
return studentname;
}
public void Setstudentname (String studentname) {
This.studentname = Studentname;
}
Public String getLocation () {
return location;
}
public void setlocation (String location) {
this.location = location;
}
Public String Gettelephone () {
return telephone;
}
public void Settelephone (String telephone) {
This.telephone = telephone;
}
Public Student (Integer ID, string studentname, string location, string telephone) {
Super ();
This.id = ID;
This.studentname = Studentname;
this.location = location;
This.telephone = telephone;
}

}

4. Create Studentdao.java

Package COM.JAVAWEB.MVC;

Import java.util.ArrayList;
Import java.util.List;
Import java.sql.*;

public class Studentdao {

Public list<student> GetAll () {
list<student> students = new arraylist<student> ();
Connection conn = null;
PreparedStatement PS = null;
ResultSet rs = null;

try {

Class.forName ("Com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/student?user=root&password=1234";
String user = "root";
String password = "1234";
String sql = "SELECT * from Stu";
conn = drivermanager.getconnection (URL, user, password);
PS = conn.preparestatement (SQL);
rs = Ps.executequery ();
while (Rs.next ()) {
int id = rs.getint (1);
String studentname = rs.getstring (2);
String location = rs.getstring (3);
String telephone = rs.getstring (4);

Student Student = new Student (ID, studentname, location, telephone);

Students.add (student);
}

}catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (rs! = null)
Rs.close ();
if (PS! = null)
Ps.close ();
IF (conn! = null)
Conn.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

return students;
}

}

5. Create a new students.jsp in WebContent

<%--Show Student Information--%>
<% @page import= "Java.util.List"%>
<% @page import= "Com.javaweb.mvc.Student"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>


<%
list<student> students = (list<student>) request.getattribute ("Students");

%>
<table>
<tr>
<th>id</th>
<th>studentName</th>
<th>location</th>
<th>telephone</th>
</tr>

<%
for (Student student:students) {
%>
<tr>
<td><%= Student.getid ()%></td>
<td><%= Student.getstudentname ()%></td>
<td><%= student.getlocation ()%></td>
<td><%= Student.gettelephone ()%></td>
</tr>
<%
}
%>

</table>

</body>

6. Add the JDBC Driver

Add the Lib folder because the Lib folder holds the jar files that support the Web app to run

7. Next, you can run the

This is the result of the operation:

Javaweb

Javaweb Development---MVC case query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.