3個jsp文件,第一個是login.jsp,第二個是judge.jsp,第三個是afterLogin.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登錄頁面</title>
</head>
<body>
<form name="loginForm" method="post" action="judgeUser.jsp">
<table>
<tr>
<td>用戶名:<input type="text" name="userName" id="userName"></td>
</tr>
<tr>
<td>密碼:<input type="password" name="password" id="password"></td>
</tr>
<tr>
<td><input type="submit" value="登錄" style="background-color:pink"> <input type="reset" value="重置" style="background-color:red"></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>身份驗證</title>
</head>
<body>
<%
request.setCharacterEncoding("GB18030");
String name = request.getParameter("userName");
String password = request.getParameter("password");
if(name.equals("abc")&& password.equals("123")) {
%>
<jsp:forward page="afterLogin.jsp">
<jsp:param name="userName" value="<%=name%>"/>
</jsp:forward>
<%
}
else {
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登錄成功</title>
</head>
<body>
<%
request.setCharacterEncoding("GB18030");
String name = request.getParameter("userName");
out.println("歡迎你:" + name);
%>
</body>
</html>
『貳』 求大神寫一下jsp的簡單的注冊界面代碼。
1.需要一個jsp頁面:
//login.jsp核心代碼:
<form action="${pageContext.request.contextPath}/servlet/UserServlet" method="post">
<input type="text" name="loginname" /><input type="password" name="password"/>
<input type="submit" value="登錄"/>
</form>
2.需要一個servlet來驗證登錄信息
//UserServlet 核心代碼
class UserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String loginname = request.getParameter("loginname");
String password = request.getParameter("password");
//創建一個service來處理業務邏輯(包括查詢資料庫操作)
UserService service = new UserService();
boolean bool = service.validateUser(loginname,password);
if(!bool){
pw.println("用戶名或密碼錯誤");
}else{
pw.println("登錄成功");
}
}
3.需要一個service處理業務邏輯(包括查詢資料庫操作)
//UserService 核心代碼
public class UserService{
/**
*查詢資料庫驗證用戶是否存在,返回boolean
*/
public boolean validateUser(String loginname,String password){
boolean bool = false;
Connection conn = null;
PreparedStatement ps = null;
//這里以mysql為例
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
String sql = "select login_name,pass_word from t_user where login_name=? and pass_word=?";
ps = conn.prepareStatement(sql);
ps.setString(0, loginname);
ps.setString(1, password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
bool = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(conn != null){
conn.close();
conn = null;
}
if(ps != null){
ps.close();
ps = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return bool;
}
}
『叄』 我想在jsp頁面里彈出一個層用dialog 如何實現 要簡單代碼
jsp中可以利用div+css來實現dialog彈出:
CSS代碼如下:
#div
{
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
z-index:999;
}
#div .layer
{
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
z-index:1000;
background: gray;
filter:alpha(opacity=60);
-moz-opacity: 0.6; /*Firefox私有,透明度50%*/
opacity: 0.6; /*其他,透明度50%*/
}
#div #center
{
position: absolute;
left: 200px;
right: 200px;
top: 70px;
bottom: 400px;
width:170px;
background:rgb(255,255,255);
margin:100px auto;
padding:60px 100px 100px 100px;
z-index: 1001;
}
DIV代碼如下:
<div id="div" style="display:none">
<div class="layer">
</div>
<div id="center">
點我彈出:
</div>
</div>
『肆』 JSP編寫一個登陸界面
1、首來先准備Dreamweaver8軟體,解自壓安裝。如下圖所示:這件點擊安裝程序,然後輸入序列號就可以了。
『伍』 寫一個JSP頁面
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'student.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>
<%
String sql="select * from user";
String className="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/jwxt";
String user="root";
String password="root";
Connection conn;
Statement st;
Class.forName(className);
conn=DriverManager.getConnection(url, user, password);
st = (Statement) conn.createStatement(); //創建用於執行靜態sql語句的Statement對象,st屬局部變數
ResultSet rs = st.executeQuery(sql); //執行sql查詢語句,返回查詢數據的結果集
%>
<table width="300" align="center" border="0" cellpadding="4" cellspacing="1" bgcolor="#cccccc">
<tr bgcolor="#EEEEEE"><td>姓名</td><td>密碼</td></tr>
<% while (rs.next()) { %>
<tr bgcolor="#ffffff">
<td><%=rs.getString("name") %></td>
<td><%=rs.getString("password") %></td>
</tr>
<%}
conn.close(); //關閉資料庫連接
%>
</table>
</body>
</html>
『陸』 用jsp編寫一個很簡單的登陸頁面
login.JSP
注: 由於時間倉促,程序可能有細小的問題!但絕對可以滿足你的需要了!如果你知道JSP什麼原理;那麼小的問題不是什麼問題了! 共同學習~~~
<%@ page contentType = "text/html;charset=GB2312" %>
<%@ page import = "java.sql.*" %>
<html>
<head>
<title></title>
</head>
<body bgcolor=LightBlue>
<div align="center">
<br>
<H1>歡迎光臨企業辦公平台</H1>
<form name="form1" method="post" action="" target="_self">
<table width="90%">
<tr>
<td width="50%" height="30" align="right">用戶名:</td>
<td width="50%" height="30" align="left"><input type="text" name="UserName"></td>
</tr>
<tr>
<td width="50%" height="30" align="right">密碼:</td>
<td width="50%" height="30" align="left"><input type="password" name="UserPassword"></td>
</tr>
<td width="100%" height="40" align="center" colspan="2">
<input type="submit" name="sub" value="登錄">
</td>
</tr>
</table>
</form>
</div>
<% try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:a","root","database1");
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
String name =new String(request.getParameter("
UserName").getBytes(「8859_1」));
String passwd =new String(request.getParameter("
UserPassword").getBytes(「8859_1」));
if(String_sql==null)
{
out.println("登陸名不可以為空");
out.println(「<HR>」);
}
else{
String_sql="select * from student where name ="+name+" and password="+psword+;
rs=stmt.executeQuery(String_sql)
while(rs.next())
{ out.println("登陸成功!");
out.println("您的用戶名是:"+name)
out.println("您的密碼是:"+password);
rs.close();
stmt.close();
conn.close();}
}
Catch( Exception e )
{out.println(「<font color=red size=5><B>」);
out.println(「出錯了!」);
out.println(「</B></font>」);
}%>
</body>
</html>