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>