「java登录jsp」java登录注册

博主:adminadmin 2022-12-03 03:33:05 68

本篇文章给大家谈谈java登录jsp,以及java登录注册对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

jsp登陆界面源代码

1、login.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

tdinput type="submit" value="登录" style="background-color:pink" input

type="reset" value="重置" style="background-color:red"/td

/tr

/table

/form

/body

/html

2、judge.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

%

request.setCharacterEncoding("GB18030");

String name = request.getParameter("userName");

String password = request.getParameter("password");

if(name.equals("abc") password.equals("123")) {

3、afterLogin.jsp文件

%

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

扩展资料:

java web登录界面源代码:

1、Data_uil.java文件

import java.sql.*;

public class Data_uil

{

public  Connection getConnection()

{

try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

}catch(ClassNotFoundException e)

{

e.printStackTrace();

}

String user="***";

String password="***";

String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=***";

Connection con=null;

try{

con=DriverManager.getConnection(url,user,password);

}catch(SQLException e)

{

e.printStackTrace();

}

return con;

}

public  String selectPassword(String username)

{

Connection connection=getConnection();

String sql="select *from login where username=?";

PreparedStatement preparedStatement=null;

ResultSet result=null;

String password=null;

try{

preparedStatement=connection.prepareStatement(sql);

preparedStatement.setString(1,username);

result=preparedStatement.executeQuery();//可执行的     查询

if(result.next())

password=result.getString("password");

}catch(SQLException e){

e.printStackTrace();

}finally

{

close(preparedStatement);

close(result);

close(connection);

}

System.out.println("找到的数据库密码为:"+password);

return password; 

}

public  void close (Connection con)

{

try{

if(con!=null)

{

con.close();

}

}catch(SQLException e)

{

e.printStackTrace();

}

}

public  void close (PreparedStatement preparedStatement)

{

try{

if(preparedStatement!=null)

{

preparedStatement.close();

}

}catch(SQLException e)

{

e.printStackTrace();

}

}

public  void close(ResultSet resultSet)

{

try{

if(resultSet!=null)

{

resultSet.close();

}

}catch(SQLException e)

{

e.printStackTrace();

}

}

}

2、login_check.jsp:文件

%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8"

title验证用户密码/title

/head

body

jsp:useBean id="util" class="util.Data_uil" scope="page" /

%

String username=(String)request.getParameter("username");

String password=(String)request.getParameter("password");

if(username==null||"".equals(username))

{

out.print("script language='javaScript' alert('用户名不能为空');/script");

response.setHeader("refresh", "0;url=user_login.jsp");

}

else

{

System.out.println("输入的用户名:"+username);

String passwordInDataBase=util.selectPassword(username);

System.out.println("密码:"+passwordInDataBase);

if(passwordInDataBase==null||"".equals(passwordInDataBase))

{

out.print("script language='javaScript' alert('用户名不存在');/script");

response.setHeader("refresh", "0;url=user_login.jsp");

}

else if(passwordInDataBase.equals(password))

{

out.print("script language='javaScript' alert('登录成功');/script");

response.setHeader("refresh", "0;url=loginSucces.jsp");

}

else

{

out.print("script language='javaScript' alert('密码错误');/script");

response.setHeader("refresh", "0;url=user_login.jsp");

}

}

%

/body

/html

3、loginSucces.jsp文件

%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"

titleInsert title here/title

/head

body

hr size="10" width="26%" align="left" color="green"

font size="6" color="red" 登录成功 /font

hr size="10" width="26%" align="left" color="green"

/body

/html

4、user_login.jsp文件

%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"

title登录界面/title

/head

body  background="C:\Users\win8\workspace\Login\image\9dcbdc339e72a5663b5c289fb5573c13_10.jpg"

center

brbrbrbrbrbr

h1 style="color:yellow"Login/h1

br

form name="loginForm" action="login_check.jsp" method="post" 

table Border="0"

tr

td账号/td

tdinput type="text" name="username"/td

/tr

tr

td密码/td

tdinput type="password" name="password"

/td

/tr

/table

br

input type="submit" value="登录" style="color:#BC8F8F"

/form

/center

/body

/html

java自动登陆JSP网站

有httpunit这个框架可以实现,代码如下

import java.io.IOException;

import java.net.MalformedURLException;

import org.xml.sax.SAXException;

import com.meterware.httpunit.GetMethodWebRequest;

import com.meterware.httpunit.PostMethodWebRequest;

import com.meterware.httpunit.WebConversation;

import com.meterware.httpunit.WebRequest;

import com.meterware.httpunit.WebResponse;

public class LoginTest {

/**

* form的get方法登录

* @throws MalformedURLException

* @throws IOException

* @throws SAXException

*/

public void getMethod() throws MalformedURLException, IOException, SAXException{

// 建立一个WebConversation实例

WebConversation wc = new WebConversation();

// 向指定的URL发出请求

WebRequest req = new GetMethodWebRequest( " " );

// 给请求加上参数

req.setParameter("username","admin");

req.setParameter("password","admin");

// 获取响应对象

WebResponse resp = wc.getResponse( req );

// 用getText方法获取相应的全部内容

// 用System.out.println将获取的内容打印在控制台上

System.out.println( resp.getText() );

}

/**

* form的post方式登录

* @throws MalformedURLException

* @throws IOException

* @throws SAXException

*/

public void postMethod() throws MalformedURLException, IOException, SAXException{

System.out.println("使用Post方式向服务器发送数据,然后获取网页内容:");

// 建立一个WebConversation实例

WebConversation wc = new WebConversation();

// 向指定的URL发出请求

WebRequest req = new PostMethodWebRequest( " " );

// 给请求加上参数

req.setParameter("username","admin");

req.setParameter("password","admin");

// 获取响应对象

WebResponse resp = wc.getResponse( req );

// 用getText方法获取相应的全部内容

// 用System.out.println将获取的内容打印在控制台上

System.out.println( resp.getText() );

}

public static void main(String[] args) throws Exception{

LoginTest test = new LoginTest();

test.postMethod();

}

}

JAVA自动登陆JSP网站

我建议一下,如果不是必须使用Excel来做数据库的话,你最好换个数据库,Mysql和Access都可以,Excel的话需要用到附加的jar包:jxl.jar。你需要将这个包附加到工程上,还有jdbc的jar包,然后就是从Excel中取得相应的信息,步骤简略如下:

建立html/jsp登录页面,简单的form表单,form的action跳转页面为处理的jsp页面,(当然如果你要想通过javabean文件来处理返回给jsp也可以)。在处理页面中,使用request.getParameter("要得到的表单元素名称");得到用户名和密码。然后通过jxl中的读取excel表格操作,得到用户名密码,然后比对。(这个就不细说了,具体的参照api),具体显示什么看你想干什么了,随便输出点东西都可以。然后退出时,直接返回第一个登录页面,在这之前最好销毁下session,然后继续。

具体代码懒得写了。你看看api应该都能写出来,说实话用excel真的很麻烦= =。

p.s觉得答案不行不给分也没关系,只是建议,楼主加油吧

关于java登录jsp和java登录注册的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-03,除非注明,否则均为首码项目网原创文章,转载请注明出处。