实验三 动态添加下拉列表的列表项

实验三  动态添加下拉列表的列表项

一、实验目的

  1、掌握代码片段中的注释的应用;

  2、掌握JSP脚本标示---Java代码片段的应用。

二、实验内容

  1、设计教师与学生不同登陆界面,如下图;

  2、验证码随机生成;

  3、提交后分别转向教师页面和学生页面进行判断用户名和密码正确性;

  4、如果正确,3秒后,转向成功页面,否则跳回验证页面;

三、实验方法

  1、在同一页面上设计两个单选按钮:教师、学生,当点击提交按钮后,进入相应的JSP页面。当用户名及密码均正确时,进入欢迎界面;如果两者其一不正确就要提醒需要重新输入。在这些操作中,注意request内置对象的正确使用方法;

  2、类似于1,使用requestout对象;

  3、学习使用重定向方法解决实验内容3

四、实验学时:2学时

五、实验代码

文件位置:

登录界面:
登录界面 login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<script type="text/javascript">
    function reImg() {
        var img = document.getElementById("Img");
        img.src = "image.jsp?code=" + Math.random();
    }
</script>
<title>登录界面</title>
<style type="text/css">
.login{
	width:400px; 
	height:400px; 
	position:absolute; 
	top:40%; 
	left:40%; 
	margin-top:-100px; 
	margin-left:-100px;
}
.right{
	width: 44px;
    height: 300px;
	float:right;
}
.left{
	width: 400px;
    height: 400px;
	position:absolute; 
	top:40%; 
	left:35%; 
	margin-top:-100px; 
	margin-left:-100px;
}
.center{
	width:500px; 
	height:500px; 
	position:relative;
	top:60%; 
	left:35%; 
	background-image:url(images/login_right.jpg);
	background-repeat:no-repeat;
}

</style>
</head>
<body>
<div class="center">
 <div class="left">
	<img src="images/login_ico2.gif">
 </div>
 <div  class="login">
  <form action="Login_check.jsp" method="post">
   <table>
    <tr>
	 <td>
	 用户名:
	 </td>
	 <td>
	 <input type="text" name="username">
	 </td>
     </tr>
     <tr>
      <td>密码:</td>
        <td>
         <input type="password" name="password" >
	 	</td>
     </tr>
	  <tr>
      <td>验证码:</td>
        <td>
         <input type="password" name="Yzm" size=4><img border=0
                        id="Img" src="image.jsp" alt="验证码"><a href="#"
                        οnclick="reImg();">看不清 </a>

	 	</td>
     </tr>
	 <tr>
       <td>
	   <input type=radio name=type value=partment>部门
	   <input type=radio name=type value=teacher>教师 
	   </td>
	   <td>
	   <input type=radio name=type value=student>学生 
	   <input type=radio name=type value=guest>游客
	   </td>
     </tr>
	 <tr>
	   <td>
	   <input type="image" src="images/login_in.gif" name="submit" alt="登录" > 
       </td>
       <td>
	   <input type="image" src="images/login_res.gif" alt="重置"  οnclick="reset();return false;" /> 
        </td>
     </tr>
   </table>
  </form>
 </div>
</div>
</body>
</html>

图片验证 image.jsp:
<%@ page contentType="image/JPEG"  
    import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"  
    pageEncoding="GBK"%>  
<%!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);   
    }%>  
<%   
    //设置页面不缓存   
    response.setHeader("Pragma", "No-cache");   
    response.setHeader("Cache-Control", "no-cache");   
    response.setDateHeader("Expires", 0);   

    // 在内存中创建图象   
    int width = 60, height = 20;   
    BufferedImage image = new BufferedImage(width, height,   
            BufferedImage.TYPE_INT_RGB);   

    // 获取图形上下文   
    Graphics g = image.getGraphics();   

    //生成随机类   
    Random random = new Random();   

    // 设定背景色   
    g.setColor(getRandColor(200, 250));   
    g.fillRect(0, 0, width, height);   

    //设定字体   
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));   

    //画边框   
    //g.setColor(new Color());   
    //g.drawRect(0,0,width-1,height-1);   

    // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
    g.setColor(getRandColor(160, 200));   
    for (int i = 0; i < 100; 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);   
    }   

    // 取随机产生的认证码(4位数字)   
    String sRand = "";   
    for (int i = 0; i < 4; i++) {   
        String rand = String.valueOf(random.nextInt(10));   
        sRand += rand;   
        // 将认证码显示到图象中   
        g.setColor(new Color(20 + random.nextInt(110), 20 + random   
        .nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
        g.drawString(rand, 13 * i + 6, 16);   
    }   

    // 将认证码存入SESSION   
    session.setAttribute("code", sRand);   

    // 图象生效   
    g.dispose();   

    // 输出图象到页面   
    ImageIO.write(image, "JPEG", response.getOutputStream());   
%>

登录验证界面 Login_check.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String name = request.getParameter("username");
	String passwd = request.getParameter("password");
	String type = request.getParameter("type");
	String yzm = request.getParameter("Yzm");
    String code = (String) session.getAttribute("code");
   	if(yzm.equals(code)){
   		if(type.equals("teacher") && name.equals("123") && passwd.equals("123")){
   			
   			session.setAttribute("name", name);
   		    session.setAttribute("passwd", passwd);
   		    session.setAttribute("type", type);
   	        out.print("<script>alert('验证成功!');</script>");
   	        response.sendRedirect("teacher.jsp");
   	    } 
   		else
   	    {
   	    	out.print("<script>alert('请正确填写信息!');window.location.href='login.jsp'</script>");
   	    }
   		if(yzm.equals(code)&&type.equals("student")&&name.equals("456")&&passwd.equals("456")){	
   	     	session.setAttribute("name", name);
   	        session.setAttribute("passwd", passwd);
   	        session.setAttribute("type", type);
   	        out.print("<script>alert('验证成功!');</script>");
   	        response.sendRedirect("student.jsp");
   	    } 
   		else
   	    //登录失败,回到Login.jsp页面。
   	    {
   	    	out.print("<script>alert('请正确填写信息!');window.location.href='login.jsp'</script>");
   	    }
   		
   	}
   	else
	    {
	    	out.print("<script>alert('请正确填写信息!');window.location.href='login.jsp'</script>");
	    }
	
	
	
	
%>

教师界面 teather.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>欢迎教师登录</title>
</head>
<body>
<table boder=0>
<tr>
	<th align="left">用户名:</th>
	<td><%=session.getAttribute("name") %></td>
</tr>
<tr>
	<th align="left">密码:</th>
	<td><%=session.getAttribute("passwd") %></td>
</tr>
<tr>
	<th align="left">登录身份:</th>
	<td><%=session.getAttribute("type") %></td>
</tr>
</table>
</body>
</html>

学生界面 student.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>学生登录界面</title>
</head>
<body>
<table boder=0>
<tr>
	<th align="left">用户名:</th>
	<td><%=session.getAttribute("name") %></td>
</tr>
<tr>
	<th align="left">密码:</th>
	<td><%=session.getAttribute("passwd") %></td>
</tr>
<tr>
	<th align="left">登录身份:</th>
	<td><%=session.getAttribute("type") %></td>
</tr>
</table>
</body>
</html>


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 首先,要掌握Java Swing图形用户界面编程,需要具备一定的Java编程基础。接下来,我将简单介绍一下实验步骤: 1. 实现页面菜单 在Swing中,可以使用JMenuBar、JMenu和JMenuItem来实现页面菜单。首先创建一个JMenuBar对象,然后在其上添加JMenu对象,最后在JMenu对象上添加JMenuItem对象。具体代码如下: ``` JMenuBar menuBar = new JMenuBar(); // 创建菜单栏 JMenu menu = new JMenu("菜单"); // 创建菜单 JMenuItem item1 = new JMenuItem("按名字读取"); // 创建菜单 JMenuItem item2 = new JMenuItem("按学号读取"); // 创建菜单 JMenuItem item3 = new JMenuItem("按班级读取"); // 创建菜单 menu.add(item1); // 添加菜单 menu.add(item2); // 添加菜单 menu.add(item3); // 添加菜单 menuBar.add(menu); // 添加菜单到菜单栏 ``` 2. 实现搜索框 在Swing中,可以使用JTextField和JButton组合实现搜索框。在JTextField中输入查询条件,点击JButton按钮后进行查询操作。具体代码如下: ``` JTextField searchField = new JTextField(20); // 创建文本框,用于输入查询条件 JButton searchButton = new JButton("搜索"); // 创建按钮,用于触发查询操作 searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String keyword = searchField.getText(); // 获取查询条件 // 根据查询条件进行查询操作 } }); ``` 3. 实现班级下拉列表 在Swing中,可以使用JComboBox组件实现下拉列表。首先创建一个JComboBox对象,然后向其中添加班级选,最后在选改变时触发查询操作。具体代码如下: ``` JComboBox<String> classComboBox = new JComboBox<String>(); // 创建下拉列表,用于选择班级 classComboBox.addItem("一班"); // 添加班级选 classComboBox.addItem("二班"); // 添加班级选 classComboBox.addItem("三班"); // 添加班级选 classComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String className = (String) classComboBox.getSelectedItem(); // 获取选中的班级 // 根据班级进行查询操作 } }); ``` 4. 实现学生信息展示功能 在Swing中,可以使用JTable组件实现格展示学生信息。首先创建一个JTable对象,并将数据填充到其中,然后将JTable添加到JScrollPane中,最后将JScrollPane添加到页面中。具体代码如下: ``` String[] columnNames = {"学号", "姓名", "班级", "性别", "成绩"}; // 定义格列名 Object[][] rowData = {{"001", "张三", "一班", "男", 90}, {"002", "李四", "二班", "女", 85}, {"003", "王五", "三班", "男", 92}}; // 定义数据 JTable table = new JTable(rowData, columnNames); // 创建格 JScrollPane scrollPane = new JScrollPane(table); // 创建滚动面板,将添加到其中 frame.add(scrollPane); // 将滚动面板添加到页面中 ``` 以上就是Java Swing图形用户界面编程的实验步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值