对于servlet初步理解
程序员文章站
2022-03-24 23:45:52
servlet在mvc框架中经常担当控制器(controller)的角色,其作用类似于调度员,下面我们就根据一个简单javaee的示例来理解servlet的机制和原理.
树结构如上,其中...
servlet在mvc框架中经常担当控制器(controller)的角色,其作用类似于调度员,下面我们就根据一个简单javaee的示例来理解servlet的机制和原理.
树结构如上,其中
login.jsp作为测试用户登录页面.
welcome.jsp作为登录成功页面
dadao.java作为数据库操作类
loginservlet.java就是我们所说的servlet类
我们来看具体内容:
login.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "https://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; utf-8"> <title>连接数据库范例</title> </head> <body> <span style="color: blue; font-weight: bold;"> <% if (request.getattribute("err") != null) { out.println(request.getattribute("err") + "<br/>"); } %> </span> 请输入用户名密码: <form id="login" method="post" action="login"> 用户名: <input type="text" name="username"><br /> 密码:<input type="password" name="password"><br /> <input type="submit" value="登录"><br /> </form> </body> </html>
以上页面只负责传递两个参数,一个是用户名,另一个是用户密码,将参数传递给loginservlet的实例中,再通过验证数据库进行结果判断,然后servlet将结果视图反馈给用户.
welcome.
<%@ page contenttype="text/html; charset=gbk" language="java" errorpage="" %>
欢迎登录
<%=session.getattribute("name")%>,欢迎登录!
此页面是用户密码验证成功后返回的视图.
dbdao.java
package glq; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; public class dbdao { private connection conn; private string driver; private string url; private string username; private string password; public dbdao() { } public dbdao(string driver, string url, string username, string password) { this.driver = driver; this.url = url; this.username = username; this.password = password; } public string getdriver() { return driver; } public void setdriver(string driver) { this.driver = driver; } public string geturl() { return url; } public void seturl(string url) { this.url = url; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public connection getconnection() throws exception { if(conn==null){ class.forname(this.driver); conn=drivermanager.getconnection(url, username, password); } return conn; } public boolean insert(string sql,object... args) throws exception, throwable{ preparedstatement ps=getconnection().preparestatement(sql); for(int i=0;i
loginservlet.java
package glq; import java.io.ioexception; import java.sql.resultset; import javax.servlet.requestdispatcher; 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 javax.servlet.http.httpsession; @webservlet(name="login" ,urlpatterns={"/login"}) public class loginservlet extends httpservlet { /** * */ private static final long serialversionuid = 1l; @override protected void service(httpservletrequest req, httpservletresponse res) throws servletexception, ioexception { string errmsg = ""; requestdispatcher rd; string username = req.getparameter("username"); string password = req.getparameter("password"); try { //创建数据库构造函数 //注意,连接时要在数据库中创建名为servlet的数据库 dbdao dd = new dbdao("com.mysql.jdbc.driver", "jdbc:mysql://localhost:3306/servlet", "root", "123456"); //执行查询方法 //注意,要在servlet数据库中添加user_inf表,并在表中添加两个属//性:name,pass resultset rs = dd.query("select pass from user_inf where name=?", username); //如果存在该用户 if (rs.next()) { //判定用户密码是否相同 if (rs.getstring("pass").equals(password)) { //获得session httpsession session = req.getsession(); //注册name值为username session.setattribute("name", username); //将参数和页面转向welcome.jsp rd = req.getrequestdispatcher("/welcome.jsp"); rd.forward(req, res); } else { //如果密码匹配失败 errmsg += "密码不对,请重新输入"; } } else { //如果没找到用户 errmsg += "用户不存在"; } } catch (exception e) { e.printstacktrace(); } //当错误信息存在时,将错误信息errmsg作为参数传递给request中的err变//量,使其在登录页面显示,并因为登录失败跳转至登录页面 if (errmsg != null && !errmsg.equals("")) { rd = req.getrequestdispatcher("/login.jsp"); req.setattribute("err", errmsg); rd.forward(req, res); } } }
在测试时,需要在web-inf的lib文件中加入mysql-connector-java-5.1.38-bin.jar包,此包为数据库工具包.
至于tomcat的构建,以及web项目的建立,需要自行检索.