本文目錄一覽:
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();
%
求jsp課本源代碼
實現會員信息管理功能,包括增加會員信息,修改會員信息,刪除會員信息,查詢(精確和模糊)會員信息功能。
JSP的源代碼寫在哪?
jsp文件是一個可以對頁面和java代碼都能操作的頁面,可以有html標籤和java代碼嵌套。java文件是你的類文件,只能有java代碼,
服務器執行jsp文件的時候其實本質是執行的java文件,我們把這種特殊的java文件稱作servlet,他具有一定固有的結構。執行過程是這樣的
首先我們要知道servlet是個什麼東西,你可以理解為他是一個java
web需要遵守的規範,他其實是一個接口interface,我們常用的tomcat就是一個servlet容器,他實現了servlet這個接口。所以我們的服務器Tomcat處理web請求的時候就是把jsp首先翻譯轉換成一個servlet文件(後綴是java的文件,只是擁有特定的格式所以叫做servlet),然後服務器執行這個servlet文件,根據servlet里的方法調用其他的java文件,根據不同的需要各種java文件定義各種不同的類和功能,最終servlet執行結束用輸出流打印一個htm文件格式輸出到頁面,瀏覽器識別這個htm顯示出來,所以我們瀏覽器顯示的並不是jsp而是jsp翻譯成servlet運行後輸出的htm
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
在JSP中怎麼實現多項選擇題,可以給以下源代碼,
你說的多項選擇題,我是不是可以理解成多選??
多選的實現是這樣的:
第一: 必須將多選框放到form裡面。
第二: 然後name屬性完全一樣,value不相同。這樣當你提交到Action中的時候,只需要使用request對象獲取toselect的值就行了。
第三: 獲取值:request.getParameterValues(“toselect”),就會將選中的多選框裡面的value獲取,並且返回一個String[]數組,這個數組裡面就有你想要的值:即選中的值
html
body
form
input type = “checkbox” value = “A” name = “toselect”/A
input type = “checkbox” value = “B” name = “toselect”/B
input type = “checkbox” value = “C” name = “toselect”/C
input type = “checkbox” value = “D” name = “toselect”/D
/form
/body
/html
原創文章,作者:IVBPT,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/330262.html