java ee开发的几种模式:model1模式,model2模式,mvc模式,SSH框架
一、model1模式
有三个jsp文件
(1)用于输入用户名和密码
(2)1 提交后进入用户处理页面
(3)如果登录成功,则进入用户登录成功页面
(1)用于输入用户名和密码
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
用户登录 <br>
<hr>
<form action="loginCl.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name = "passwd"><br>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
</body>
</html>
(2)用于验证用户名和密码,如果成功,则跳转
用response.sendRedirect 来进行跳转
<body>
<%
String u = request.getParameter("username");
String p = request.getParameter("passwd");
if(u.equals("wbx") && p.equals("wbx"))
{
response.sendRedirect("wel.jsp?user="+u);
}
else
response.sendRedirect("login.jsp");
%>
</body>
(3)
<body>
<%
String username = request.getParameter("user");
%>
welcome wbx <%=username %><br>
</body>
如何在jsp中数据库的操作:
<body>
<%
String u = request.getParameter("username");
String p = request.getParameter("passwd");
Connection cnn;
String url = "jdbc:mysql://localhost:3306/wbx1?user=root&password=buptnic&useUnicode=true&characterEncoding=UTF8";
Class.forName("com.mysql.jdbc.Driver");
cnn = DriverManager.getConnection(url);
Statement sm = cnn.createStatement();
ResultSet rs = sm.executeQuery("select password from user where name='"+u+"'");
//根据结果做判断
if(rs.next())
{
if(rs.getString(1).equals(p))
{
response.sendRedirect("wel.jsp?user="+u);
}
}
else
{
response.sendRedirect("login.jsp");
}
// if(u.equals("wbx") && p.equals("wbx"))
// {
// response.sendRedirect("wel.jsp?user="+u);
// }
// else
// response.sendRedirect("login.jsp");
%>
</body>