上海营销网站建设,台州外发加工网,网站备案 人在上海,重庆假山制作实验4#xff1a;Servlet请求与响应
1、在页面输入学生学号#xff0c;从数据库中查询学生信息并显示。
#xff08;1#xff09;启动MySQL数据库服务#xff0c;新建数据库#xff0c;将student.sql文件导入到新建数据库#xff08;建立表#xff0c;并插入3条数据Servlet请求与响应
1、在页面输入学生学号从数据库中查询学生信息并显示。
1启动MySQL数据库服务新建数据库将student.sql文件导入到新建数据库建立表并插入3条数据 2新建html页面
!DOCTYPE html
html langen xmlnshttp://www.w3.org/1999/html
headmeta charsetUTF-8titleTitle/title
/head
body
form action/weeebbbb_war_exploded/LoginServlet methodpost用户名input typetext nameusername/br密码input typepassword namepassword/brinput typesubmit value提交
/form
/body
/html
3新建DbUtil类
package com.example.weeebbbb;import java.sql.*;public class DbUtil {public static String DRIVER com.mysql.jdbc.Driver;//数据库public static final String URL jdbc:mysql://localhost:3306/student?useSSLfalse;public static final String DBUser root;public static final String DBPassword 123456;public static Connection conn null;public static Connection getConn(String dbDri,String dbUrl,String username,String pass) {try {Class.forName(DRIVER);conn DriverManager.getConnection(URL, DBUser, DBPassword);} catch (Exception e) {e.printStackTrace();}return conn;}
}4在web.xml文件中配置数据库连接信息 context-paramparam-namedbUrl/param-nameparam-valuejdbc:mysql://localhost:3306/student/param-value/context-paramcontext-paramparam-nameduUsername/param-nameparam-valueroot/param-value/context-paramcontext-paramparam-namedbPassword/param-nameparam-valueroot/param-value/context-paramcontext-paramparam-namejdbcDriver/param-nameparam-valuecom.mysql.jdbc.Driver/param-value/context-param
5在pom.xml文件中添加MySQL驱动依赖需要联网一定要考虑自己数据库的版本注意驱动版本和web.xml文件中的jdbcDriver驱动字符串的写法则项目可不用导入MySQL驱动jar包 dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.49/version
/dependency
6新建Servlet完成获取网页表单提交的学号信息并从数据库中查询此学号学生的信息并响应输出
package com.example.weeebbbb;import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;WebServlet(name wek4_queryServlet,value /wek4_queryServlet)
public class wek4_queryServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println(1111);resp.setContentType(text/html;charsetutf-8);String stuNumreq.getParameter(stuNum);ServletContext contextthis.getServletContext();String dbUrlcontext.getInitParameter(dbUrl);String dbUsernamecontext.getInitParameter(dbUsername);String dbPasswordcontext.getInitParameter(dbPassword);String jdbcDrivercontext.getInitParameter(jdbcDriver);Connection connectionDbUtil.getConn(dbUrl,dbUsername,dbPassword,jdbcDriver);String sqlselect * from student where stuNum?;PreparedStatement pstnull;try{pst connection.prepareStatement(sql);pst.setString(1,stuNum);ResultSet rspst.executeQuery();String str;System.out.println(11111);if(rs.next()){System.out.println(22222);str这位同学的学号是rs.getString(stuNum),姓名是rs.getString(stuName);}else{str查无此人;}resp.getWriter().println(str);}catch (SQLException e){e.printStackTrace();}}
}7新建输入信息页面
%--Created by IntelliJ IDEA.User: cicDate: 2023/11/8Time: 14:19To change this template use File | Settings | File Templates.
--%
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headmeta charsetUTF-8titleTitle/title
/head
body
form actionwek4_queryServlet methodget请输入学号input typetext size20 namestuNuminput typesubmit value查询
/form/body
/html查询已有数据 查询成功 查询不存在的数据 查询失败