本文目录一览:
java jsp页面怎么写java类?怎么调用java代码?
jsp中写java代码成为scriptlet,写在%%之间就可以了。
Scriptlet是包含在%和%之间的Java代码,在Web容器处理JSP页面时执行,通常会产生输出,并将输出发送到客户的输出流里。Scriptlet除了不能定义类和方法、不能用import引入类外,可以包含任何有效的Java代码。(Java类在Jsp外部定义,可用page指令的import属性引入,也可以Java Bean的形式使用。Java中的方法必须在类内定义,但Jsp允许使用声明定义方法。窗体(GUI)设计代码在Jsp中无效)。
Scriptlet例程:
%@ page contentType="text/html; charset=gb2312" %
html
head
titleJSP基本语法/title
/head
body
h1Scriptlet示例页面/h1
table border="1"
caption乘法口诀表/caption
%-- 在网页中嵌入Java代码的主要方法 --%
%
for(int i=1; i=9; i++) {
int j=1;
//out是JSP的一个内部对象,print方法用于向客户端输出数据
out.println("tr");
for(; j=i; j++) {
out.print("td" + j + "*" + i + "=" + j*i + "/td");
}
for(;j=9;j++) {
out.print("td /td");
}
out.println("/tr");
}
%
/table
/body
/html
jsp如何设置验证码,我需要完成详细的代码和过程
A 生成验证码的java文件
package cn.com.data100.web.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Title: 生成验证码
* Description: data100 website
* Copyright: Copyright (c) 2011
* Company: km100
* @author anshufa
* @version 1.0
*/
public class RandomCodeServlet extends HttpServlet {
private static final Log log = LogFactory.getLog(RandomCodeServlet.class);
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc 255)
fc = 255;
if (bc 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
Random random = new Random();
// 定义数组存放加减乘除四个运算符
// char[] arr = { '+', '-','*', '/' };
char[] arr = { '+', '-','*'};
// 生成10以内的随机整数num1
int num1 = random.nextInt(10);
// 生成一个0-4之间的随机整数operate
int operate = random.nextInt(3);
// 生成10以内的随机整数num1
int num2 = random.nextInt(10);
//避免出现负数情况
if(operate == 1){//减法运算
//如果减数小于被减数 再次生成num1
while(num1num2){
num1 = random.nextInt(10);
}
}
// // 避免出现除数为0的情况
// if (operate == 3) {
// // 如果是除法,那除数必须不能为0,如果为0,再次生成num2
// while (num2 == 0) {
// num2 = random.nextInt(10);
// }
// }
// 运算结果
int result = 0;
// 假定position值0/1/2/3分别代表"+","-","*","/",计算前面操作数的运算结果
switch (operate) {
case 0:
result = num1 + num2;
break;
case 1:
result = num1 - num2;
break;
case 2:
result = num1 * num2;
break;
// case 3:
// result = num1 / num2;
// break;
}
log.info("验证码:"+num1 + "," + num2 + "," + operate + "," + result);
// 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。
HttpSession session = request.getSession();
session.setAttribute("rand", String.valueOf(result));
int width = 60, height = 20;
//创建BufferedImage对象,设置图片的长度宽度和色彩。
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
//取得Graphics对象,用来绘制图片
Graphics g = image.getGraphics();
//绘制图片背景和文字,释放Graphics对象所占用的资源。
g.setColor(getRandColor(199, 250));
//设置内容生成的位置
g.fillRect(0, 0, width, height);
//设置内容的字体和大小
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//设置内容的颜色:主要为生成图片背景的线条
g.setColor(getRandColor(160, 200));
//图片背景上随机生成155条线条,避免通过图片识别破解验证码
for (int i = 0; i 355; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
//构造运算表达式
String content = num1+" "+ arr[operate]+" "+num2+" = ";
//设置写运算表达的颜色
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//在指定位置绘制指定内容(即运算表达式)
g.drawString(content,5,16);
//释放此图形的上下文以及它使用的所有系统资源,类似于关闭流
g.dispose();
//通过ImageIO对象的write静态方法将图片输出。
ImageIO.write(image, "JPEG", os);
os.close();
}
}
B 页面调用
span class="tabboder" id="rands"img
src="../RandomCodeServlet" id="auth" name="auth" border=0/span
jsp中显示验证码的代码怎么写?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/*生成验证码图片
*/
public class MakeCertPic {
//验证码图片中可以出现的字符集,可以根据需要修改
private char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'
};
/* 功能:生成彩色验证码图片
参数wedth为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
public String getCertPic(int width,int height,OutputStream os){
if(width=0)
width=60;
if(height=0)
height=20;
BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//设定背景颜色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0,0,width,height);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//随机产生的验证码
String strEnsure = "";
//4代表4为验证码,如果要产生更多位的验证码,则加大数值
for(int i = 0;i4;++i){
strEnsure += mapTable[(int) (mapTable.length*Math.random())];
}
//将认证码显示到图像中,如果要生成更多位的验证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str, 20, 15);
str = strEnsure.substring(2,3);
g.drawString(str, 35, 18);
str = strEnsure.substring(3,4);
g.drawString(str, 45, 15);
//随机产生15个干扰点
Random rand = new Random();
for(int i=0; i10; i++){
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
//释放图形上下文
g.dispose();
try{
//输出图形到页面
ImageIO.write(image, "JPEG", os);
}catch (IOException e){
return "";
}
return strEnsure;
}
}
makeCertPic.jsp页面用于调用生成验证码图片的JavaBean,并在客户端显示,源代码如下:
%@page contentType="image/jpeg" %%@page language="java" pageEncoding="utf-8"%jsp:useBean id="image" scope="page" class="securityCode.pic.MakeCertPic"/%
String str = image.getCertPic(0,0,response.getOutputStream());
//将验证码存入session中
session.setAttribute("certCode",str);
%
下边是登录页面:
%@ 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
script type="text/javascript"
function changeimg()
{
var myimg = document.getElementById("code");
now = new Date();
myimg.src="makeCertPic.jsp?code="+now.getTime();
}
/script
/head
body
center
form action="loginCheck.jsp" method="post" /
用户名:input type="text" name="username" /br
密nbsp;nbsp;码:input type="password" name="password"/br
nbsp;验证码:input type="text" name="certCode"/
img id="code" src="makeCertPic.jsp"a href="javascript:changeimg()"看不清,换一张 /abr
input type="submit" value="登录"/
/form
/center
/body
/html
在jsp中验证代码怎么写和调用
jsp中的验证一般都用javascript来验证的。如果不对的话可能你的问题表达不清楚,是指表单验证还是逻辑验证。把问题描述的更清楚点。