Ⅰ 如何用eclipse写登录注册页面的代码
java写的用户登录实例,实际页面展示使用的jsp,那么下面是jsp的登录页面代码:
1、login.jsp代码
<%
string name = request.getparameter("username");
string pwd = request.getparameter("password");
//out.println(name+pwd);
string sql ="select * from info where username='"+name+"' and password='"+pwd+"'";
//out.println(sql);
statement stm= null;
resultset rs =null;
try
{
stm = conn.createstatement();
rs = stm.executequery(sql);
if(rs.next())
{
session.setattribute("username",name);
response.sendredirect("index.html");
}
else
{
response.sendredirect("index1.html");
}
}
catch(sqlexception e)
{
e.printstacktrace();
}
%>
<!--登录的表单-->
<form name="form1" method="post" action="login.jsp">
<p>
<label for="username"></label> 用户名
<input type="text" name="username" id="username">
</p>
<p>
<label for="passwrod"></label> 密码
<input type="text" name="passwrod" id="passwrod">
</p>
<p>
<input type="submit" name="button" id="button" value="提交">
</p>
</form>
2、用户信息表,存放用户名和密码:
user_info 表
create table if not exists `test` (
`id` int(8) not null auto_increment,
`username` char(150) default null,
`password` varchar(32),
`times` int(4) not null,
primary key (`id`)
) engine=myisam default charset=utf8 auto_increment=1 ;
Ⅱ JSP编写一个登陆界面
1、首来先准备Dreamweaver8软件,解自压安装。如下图所示:这件点击安装程序,然后输入序列号就可以了。
Ⅲ 求大神写一下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代码编写下载程序,就是那种网页普遍都有的那种下载界面,求高手解答~!
在上传文件时,记录文件路径等信息
前台需要下载时,获取文件的名称,路径的信息。
简单来讲就是这么回事儿
Ⅳ 编写用户注册于登录的JSP页面的全部程序代码
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页面上实现点击注册按钮,弹出一个窗体来注册(类似于百度贴吧的登录和注册),求详细代码和注释
jsp中的注册弹出新窗口是通过window.open一个新页面来实现的。
页面register.jsp代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="cn.wy.Pet.User" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>会员注册例子讲解</title>
<style type="text/css">
<!--
.STYLE1 {
color: #FF0000;
font-weight: bold;
}
.STYLE2 {color: #FF0000}
.STYLE3 {
font-size: 18px;
font-weight: bold;
}
-->
</style>
</head>
<body style="font-size:12px">
<form id="form1" name="form1" method="post" action="<%=actionStr%>reg">
<p align="center"><br />
<span class="STYLE3">用户注册</span></p>
<table width="582" height="302" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#BCACD2">
<tr>
<td width="80" align="right">用户名:</td>
<td width="496" align="left"><input name="userName" type="text" id="userName" size="16" maxlength="16" />
<span class="STYLE1">*</span> 3~16位字母或者数字(如:8hack)</td>
</tr>
<tr>
<td align="right">密码:</td>
<td align="left"><input name="password1" type="text" id="password1" size="16" maxlength="16" />
<span class="STYLE1">* </span> 3~16位字母或者数字(如:abc123)</td>
</tr>
<tr>
<td align="right">确认密码:</td>
<td align="left"><input name="password2" type="text" id="password2" size="16" maxlength="16" />
<span class="STYLE1">*</span> 必须和上面输入的密码相同</td>
</tr>
<tr>
<td align="right">电子邮件:</td>
<td align="left"><input name="email" type="text" id="email" maxlength="20" />
<span class="STYLE1">*</span> 找回密码和联系用(如:[email protected])</td>
</tr>
<tr>
<td align="right">联系电话:</td>
<td align="left"><input name="tel" type="text" id="tel" size="20" maxlength="20" />
如(0871-8888888,13888853113)</td>
</tr>
<tr>
<td align="right">联系地址:</td>
<td align="left"><input name="address" type="text" id="address" maxlength="50" /></td>
</tr>
<td height="40" colspan="2" align="center"><input type="submit" name="Submit" value="确认注册" />
<input type="reset" name="Submit2" value="重新填写" /></td>
</tr>
</table>
</form>
</body>
</html>
后台servlet的处理:
public class reg extends HttpServlet
{
public reg()
{
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
DBConnection dbc=null;
String userName;
String psd;
String email;
String tel;
String address;
int popedom;
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
try{
dbc = new DBConnection();
PreparedStatement ps = null;
userName = request.getParameter("userName");
psd = login.encrypt(request.getParameter("password1").toString());
email = request.getParameter("email");
tel = request.getParameter("tel");
address = request.getParameter("address");
popedom = Integer.parseInt(request.getParameter("popedom"));
if (userName != null && psd != null && email != null)
{
ps = dbc.getCon().prepareStatement("insert into [User](UName,Upass,UEmail,UTel,UAddress,UPopedom) values(?,?,?,?,?,?)");
ps.setString(1, userName);
ps.setString(2, psd);
ps.setString(3, email);
ps.setString(4, tel);
ps.setString(5, address);
ps.setInt(6, popedom);
ps.execute();
System.out.print("新用户注册:" + request.getParameter("userName") + " ");
out.print("<script>alert('恭喜您:注册成功!现已经登录到网站!');history.go(-1)</script>");
}
if (dbc != null)
dbc.dbClose();
}
catch(SQLException ex)
{
out.print("<script>alert('注册失败!数据库连接错误!');history.go(-1)</script>");
ex.printStackTrace();
if (dbc != null)
dbc.dbClose();
}
}
}