jsp参考大全及源代码(jsp的代码)

  • 1、jsp技术的验证码源代码?
  • 2、jsp登陆界面源代码
  • 3、jsp 中网站的首页源代码

实际当中很少用这种纯JSP的验证码技术,缺乏安全性,下面有一个这样的纯JSP的例,参考别人的,你可以看看。

纯数字验证码

在JSP页面body区域编写如下代码:

导入java.awt.*,java.awt.image.BufferedImage,javax.imageio.ImageIO包

%

   //验证码边框的长高

   int width = 60;

   int height = 20;

   //用RGB模式输出图像区域

   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

   //定义画笔

   Graphics graph = image.getGraphics();

   //设置验证码框背景色0-255

   graph.setColor(new Color(200, 200, 200));

   //填充矩形

   graph.fillRect(0, 0, width, height);

   //产生1000-9999之间的随机数

   Random rnd = new Random();

   int rndNum = rnd.nextInt(8999) + 1000;

   //此处为何转换为String型的用int型的效果一样?

   String rndStr = String.valueOf(rndNum);

   session.setAttribute(“rndStr”, rndNum);

   //设置矩形区域中随机数及干扰点的颜色

   graph.setColor(Color.RED);

   //设置随机数的字体大小

   graph.setFont(new Font(“”,Font.PLAIN,20));

   //在已有的矩形区域中绘制随机数

   graph.drawString(rndStr, 8, 17);

   //随机产生100个干扰点

   for (int i = 0; i  100; i++)

   {

    int x = rnd.nextInt(width);

    int y = rnd.nextInt(height);

    //设置干扰点的位置长宽

    graph.drawOval(x, y, 1, 1);

   }

   //将图像输出到页面上

   ImageIO.write(image, “JPEG”, response.getOutputStream());

   //清空缓冲区

   out.clear();

   out = pageContext.pushBody();

  %

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

这是最简单的一个例子,数据库要你自己建,用的是ACCESS

%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”” %

html

head

meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″

titleJSP连接Access数据库/title

style type=”text/css”

!–

.style1 {

font-size: 20px;

font-weight: bold;

}

/style

/headbody

div align=”center” class=”style1″JSP连接Access数据库/div

br

hr

p%

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); //载入驱动程序类别

Connection con = DriverManager.getConnection(“jdbc:odbc:jspdata”); //建立数据库链接,jspdata为ODBC数据源名称

//建立Statement对象

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

ResultSet rs = stmt.executeQuery(“select * from lyb”); //建立ResultSet(结果集)对象,并执行SQL语句

%

/p

p align=”center”NUMB1数据表中记录如下/p

table width=”640″ border=”1″ align=”center” bordercolor=”#7188e0″

tr bgcolor=”d1d1ff”

th width=”49″编号/th

th width=”90″姓名/th

th width=”126″E-mail/th

th width=”221″网站/th

th width=”80″QQ/th

/tr

%

while(rs.next())

{

%

tr bgcolor=”#f8f8f8″

th%= rs.getString(1) %/th

th%= rs.getString(2) %/th

th%= rs.getString(3) %/th

th bgcolor=”#f6f6f8″%= rs.getString(4) %/th

th%= rs.getString(5) %/th

/tr

%

}

rs.close();

stmt.close();

con.close();

%

/table

p align=”center”br

如果您能看到表格中的数据,说明连接数据库成功!/p

/body

/html

原创文章,作者:R7EL6,如若转载,请注明出处:https://www.506064.com/n/126767.html

相关推荐

  • js强制弹出窗口代码研究,恶作剧代码一直弹出窗口

    本文目录一览: 1、求.js文件弹出窗口代码 2、js弹出窗口的命令 3、JS弹出对话框怎么写? 4、如何用JS实现关闭浏览器窗口强制弹出广告窗口(退弹代码) 5、JavaScri…

    编程 2024-10-31
  • 含jsp样例文件,考虑下面jsp文件代码片段

    本文目录一览: 1、JSP是什么? 2、在JSP如何包含一个静态文件? 3、如何在jsp中动态包含一个jsp文件 4、.jsp是什么格式呀 ? JSP是什么? JSP(JavaSe…

    编程 2024-11-17
  • qq空间的js代码怎么加密,怎么把空间加密

    本文目录一览: 1、js如何加密源代码,页面样式全部用js写,如何操作? 2、如何加密js? 3、如何对JS代码加密 4、介绍一点js加密的方法 5、关于QQ空间GET登录密码算法…

    编程 2024-12-12
  • 代码质量检查工具详解

    一、圈复杂度 圈复杂度是指在程序的控制流图中,对于任意一个节点所围成的区域的复杂度。它是评价代码复杂度的一种重要指标,主要用于衡量代码中可能存在的缺陷的数量和程序测试的难度。 圈复…

    编程 2024-10-04
  • 几个常用js代码(js实用代码)

    本文目录一览: 1、求最最简单的JS代码 2、js中有哪几种可执行代码? 3、简单的JS代码 4、求三个常用的js代码? 求最最简单的JS代码 一楼代码没问题,报“缺少对象”可能是…

    编程 2024-10-03
  • 用C++编写的简单小游戏源代码分享

    一、游戏源代码介绍 在现代技术的支撑下,C++已经成为了一种非常流行的编程语言。并且,使用C++编写游戏的越来越普遍,这主要是因为C++具有高效、面向对象和跨平台的特性,让游戏的开…

    编程 2024-12-12
  • html免php上传代码,php 上传

    本文目录一览: 1、怎么通过HTML+PHP上传文件到服务器? 2、html上传文件代码 3、求html多文件上传代码 可参考以下代码 4、写一个html+php代码,实现客户基本…

    编程 2024-10-04
  • Python对象创建:构建可复用代码的基本单元

    Python是一门面向对象的编程语言,在Python中,一切皆为对象,每个对象都有自己的属性和方法。因此,对象可以被视为构建可复用代码的基本单元,对象的创建、使用和销毁在Pytho…

    编程 2024-12-22
  • 在线js代码编辑器插件下载(JS在线编辑器)

    本文目录一览: 1、JS 文本编辑器 2、有哪些好用的代码编辑器值得推荐? 3、有哪些好用的代码编辑器值得推荐 JS 文本编辑器 今天参加一个讲座的时候,讲师推荐了一个很好用的JS…

    编程 2024-10-03
  • VS2019快捷键大全

    一、基本使用 1、VS2019的基本使用快捷键主要是以下的几个: Ctrl + N:新建项 Ctrl + Shift + N:新建项目 Ctrl + O:打开文件 Ctrl + S…

    编程 2024-10-04