java jsp页面怎么写java类?怎么调用java代码?

java jsp页面怎么写java类?怎么调用java代码?,第1张

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>
<title>JSP基本语法</title>
</head>
<body>
<h1>Scriptlet示例页面</h1>
<table border="1">
<caption>乘法口诀表</caption>
<%-- 在网页中嵌入Java代码的主要方法 --%>
<%
for(int i=1; i<=9; i++) {
int j=1;
//out是JSP的一个内部对象,print方法用于向客户端输出数据
outprintln("<tr>");
for(; j<=i; j++) {
outprint("<td>" + j + "" + i + "=" + ji + "</td>");
}
for(;j<=9;j++) {
outprint("<td> </td>");
}
outprintln("</tr>");
}
%>
</table>
</body>
</html>

import javautilLinkedList;
import javaxswingtexthtmlHTMLDocumentIterator;
public class TestKnow {
public static void main(String[] args) {
Student s1 = new Student(1,"s1",15);
Student s2 = new Student(2,"s2",15);
Student s3 = new Student(3,"s3",15);
Grade g = new Grade(1);
gadd(s1);gadd(s2);gadd(s3);//增
gdelete(s1);//删
Student s4 = new Student(4,"s4",12);
gchange(s2, s4);//改
Student ss = gcheck(3);//查
Systemoutprintln(ssname);
}
}
class Student{
int num;//学号
String name;
int age;
public Student(int num,String name,int age){
thisnum = num;
thisname = name;
thisage = age;
}
}
class Grade{
int gnum;
LinkedList<Student> grade;
public Grade(int gnum){
thisgnum = gnum;
grade = new LinkedList<Student>();
}
public void add(Student s){
gradeadd(s);
}
public Student check(int n){
javautilIterator<Student> it = gradeiterator();
while(ithasNext()){
Student temp = itnext();
if(tempnum==n)
return temp;
}
return null;
}
public void delete(Student s){
if(grade != null)
if(check(snum)!=null)
graderemove(s);
else
Systemoutprintln("没有此人");
else
Systemoutprintln("班里没人");
}
public void change(Student s1,Student s2){
if(grade != null)
if(check(s1num)!=null)
{
graderemove(s1);
gradeadd(s2);
}
else
Systemoutprintln("没有要替换的人");
else
    Systemoutprintln("班里没人");
}
}

public class Test013 {
/
编写一个图形类MyGraphic。 1)它有两个基本属性:图形线条的颜色String lineColor和图形的填充颜色String
fillColor。 2)设计矩形类CRectangle,有属性double rLong和宽double rWidth, 使用方法 float
calCircum()可以返回矩形的周长,使用方法float calSquare()可以返回矩形的面积。
编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。 3)设计圆形类CCircle,定义属性:半径double
radius,可以通过同名方法计算周长和面积。 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。
4)编写出应用程序对CRectangle类和CCircle类进行验证。 完成上述要求即可
/
public static void main(String[] args) {
MyGraphic rectangle = new CRectangle(10, 5);
rectanglesetFillColor("紫色"); //设定矩形填充颜色
rectanglesetLineColor("白色"); //设定矩形线条颜色
rectangleshow();
Systemoutprintln("矩形周长 = " + rectanglecalCircum());
Systemoutprintln("矩形面积 = " + rectanglecalSquare());
MyGraphic circle = new CCircle(3);
circlesetFillColor("红色");
circlesetLineColor("");
circleshow();
Systemoutprintln("园形周长 = " + circlecalCircum());
Systemoutprintln("园形面积 = " + circlecalSquare());
}
}
/
图形类

/
abstract class MyGraphic {
private String lineColor; // 图形线条的颜色
private String fillColor; // 图形的填充颜色
public String getLineColor() {
return lineColor;
}
public void setLineColor(String lineColor) {
thislineColor = lineColor;
}
public String getFillColor() {
return fillColor;
}
public void setFillColor(String fillColor) {
thisfillColor = fillColor;
}
public MyGraphic(String lineColor, String fillColor) {
thislineColor = lineColor;
thisfillColor = fillColor;
}
public MyGraphic() {
}
/
显示图形的颜色
/
public abstract void show();
/
计算图形的周长
/
public abstract float calCircum();
/
计算图形的面积
/
public abstract float calSquare();
}
/
矩形类

/
class CRectangle extends MyGraphic {
private double rLong; // 长
private double rWidth; // 宽
/
通过构造函数为图形的属性赋值

@param rLong
@param rWidth
/
public CRectangle(double rLong, double rWidth) {
thisrLong = rLong;
thisrWidth = rWidth;
}
/
@return 矩形的周长
/
@Override
public float calCircum() {
return (float) (2 (rLong + rWidth));
}
/
@return 矩形的面积
/
@Override
public float calSquare() {
return (float) (rLong rWidth);
}
@Override
public void show() {
Systemoutprintln("矩形线条的颜色: " + supergetLineColor());
Systemoutprintln("矩形填充颜色: " + supergetFillColor());
}
public double getrLong() {
return rLong;
}
public void setrLong(double rLong) {
thisrLong = rLong;
}
public double getrWidth() {
return rWidth;
}
public void setrWidth(double rWidth) {
thisrWidth = rWidth;
}
}
/
圆形类

/
class CCircle extends MyGraphic {
private double radius; // 圆形半径
public CCircle(double radius) {
thisradius = radius;
}
/
@return 圆形的周长
/
@Override
public float calCircum() {
return (float) (2 MathPI radius);
}
/
@return 圆形的面积
/
@Override
public float calSquare() {
return (float) (MathPI radius radius);
}
@Override
public void show() {
Systemoutprintln("圆形线条的颜色: " + supergetLineColor());
Systemoutprintln("圆形填充颜色: " + supergetFillColor());
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
thisradius = radius;
}
}

又一个北鸟的学弟。。
public class QuessMachine {
String[] shop= {"电脑","空调","洗衣机"};
int[] price={5000,2000,3000};
int no,shu;
public int initial(){
no = (int) (Mathrandom() shoplength);
return no;
}
public void guess(){
Scanner input = new Scanner(Systemin);
for(int i=0;i<5;i++){
shu=inputnextInt();
if(shu==price[no]){
Systemoutprintln("恭喜你。正确!!!");
break;
}
if(shu<price[no]){
Systemoutprint("\n再大一点\n再猜一次:");
}
else if(i==4){
Systemoutprintln("4次内没有没有猜对,下次努力吧!");
}
else{
Systemoutprint("\n再小一点\n再猜一次:");
}
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(Systemin);
QuessMachine test=new QuessMachine();
testinitial();
Systemoutprint("请猜测 “"+testshop[testno]+"”的价格:");
testguess();
}
慢慢参考吧。。

编写Cource

/
  一、编写一个课程类Course,包含:
  1、3个私有成员变量:课程编号(cNumber)、课程名(cName)和学分数(cUnit);
  2、1个构造器方法:带3个参数的构造器方法,用于初始化课程编号、课程名和学分。
  3、成员方法:(1)cNumber 、cName、cUnit属性的set和get方法
  (2)printCourceInfo:用于输出课程的相关信息;
  4、编写Cource类的测试程序,创建课程对象:编号为070401,课程名为Java程序设计,学分为4。要求输出课
 /
public class Course {
    private int cNumber;
    private String cName;
    private int cUnit;
    public Course(int cNumber, String cName, int cUnit) {
        thiscNumber = cNumber;
        thiscName = cName;
        thiscUnit = cUnit;
    }
    public void setcNumber(int cNumber) {
        thiscNumber = cNumber;
    }
    public void setcName(String cName) {
        thiscName = cName;
    }
    public void setcUnit(int cUnit) {
        thiscUnit = cUnit;
    }
    public int getcNumber() {
        return cNumber;
    }
    public String getcName() {
        return cName;
    }
    public int getcUnit() {
        return cUnit;
    }
    public void printCourseInfo() {
        Systemoutprintln("课程编号: " + cNumber + "  课程名" + cName + "  学分数" + cUnit);
    }
}

编写测试类

public class TestCourse{
    public static void main(String[] args){
        Course course=new Course(070401,"Java程序设计",4);
        courseprintCourseInfo();
    }
 }

代码是AndroidStudio里写的。测试不了~不过肯定没错的(吧~)

用IDE写的话可以直接生成get set 和构造函数的。这种代码都不怎么需要写的。不过如果是新手的话写多点没坏处。

//手机类
class Phone{
private String brand;//品牌
private String type;//型号
//重写构造函数
public Phone(String b,String t){
thisbrand = b;
thistype = t;
}
//显示手机信息
public void show(){
Systemoutprintln("手机品牌为:"+thisbrand+" 手机型号为:"+thistype);
}
}
//测试类
public class Test {
public static void main(String[] args){
Phone p = new Phone("NOKIA","5230");//初始化手机信息
pshow();
}
}
运行结果:
手机品牌为:NOKIA 手机型号为:5230


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13341809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-18
下一篇 2023-07-18

发表评论

登录后才能评论

评论列表(0条)

保存