实验二:jsp基本语法

实验环境:Myeclipse 10

实验题目:

熟悉jsp语法

1:在jsp文档内输入一个数字,如果小于60分,打印不及格,大于60分,打印及格。(分有out无out做法)

2:利用循环输出10行相同的文字(分有out无out做法)

3:看以下图片:

index.jsp

试验分析查看效果。

4:应用java程序片段动态生成下拉列表,点击查询时,能弹出用户输入的内容:姓名,年龄,所在部门,如下图:

员工信息查询

点击查询后,能在本网页的下方显示出刚输入的员工姓名,年龄,和所在部门。

实验实现代码:

第一题(2-1.jsp):


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">



<title>2-1</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>



<body>

<%//有out做法

int number1 = 70;

if(number1>60){

out.println("及格");

}else{

out.println("不及格");

}

%>



<%//无out做法

int number2 = 70;

if(number2>60){%>

<p>及格</p>

<% }else{%>

<p>不及格</p>

<%}

%>

</body>

</html>



第二题(2-2.jsp):


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">



<title>2-2</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>



<body>

<%//有out做法

for(int i=0;i<10;i++){

out.print("相同的文字<br>");

}

%>

<%//无out做法

for(int i=0;i<10;i++){%>

<p>相同的文字</p>

<%}

%>

</body>

</html>



第三题:

实现动态输出效果,把书名按编号一行一行输出

第四题(2-4.jsp):


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">



<title>2-2</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>



<body>

<%String[] dept = {"策划部","销售部","研发部","人事部","测试部"}; %>

<h1 align="center">员工信息查询</h1>

<form id="form" name="forme" method = "post" action = "2-4.jsp">

<p align="center">

员工姓名:<input type="text" name="user_name" id="user_name" size="5">&nbsp;

年龄:<input type="text" name="user_age" id="user_age" size="3">&nbsp;

所在部门:<select name="bumen">

<%for(int i=0;i<dept.length;i++){%>

<option value="<%=dept[i] %>"><%=dept[i] %></option>

<%}%>

</select>

<input type="submit" value="查询">

</p>

</form>

<% request.setCharacterEncoding("UTF-8");%>

<br>

<br>

<br>

<br>

<p align="center">

<%

out.print("员工姓名:"+request.getParameter("user_name")+"&nbsp;&nbsp;年龄:"+request.getParameter("user_age")+"&nbsp;&nbsp;所在部门:"+request.getParameter("bumen"));

%>

</p>

</body>

</html>



问题:

第四题在不输入任何信息的前提下点击查询会出现空指针异常

优化后代码:

第四题(4-2.jsp)


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">



<title>2-2</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>



<body>

<%String[] dept = {"策划部","销售部","研发部","人事部","测试部"}; %>

<h1 align="center">员工信息查询</h1>

<form id="form" name="forme" method = "post" action = "2-4.jsp">

<p align="center">

员工姓名:<input type="text" name="user_name" id="user_name" size="5">&nbsp;

年龄:<input type="text" name="user_age" id="user_age" size="3">&nbsp;

所在部门:<select name="bumen">

<%for(int i=0;i<dept.length;i++){%>

<option value="<%=dept[i] %>"><%=dept[i] %></option>

<%}%>

</select>

<input type="submit" value="查询">

</p>

</form>

<% request.setCharacterEncoding("UTF-8");%>

<br>

<br>

<br>

<br>

<p align="center">

<%

String name = request.getParameter("user_name");

String age = request.getParameter("user_age");

String bumen = request.getParameter("bumen");

if(name != null && age != null && bumen != null){

out.print("员工姓名:"+request.getParameter("user_name")+"&nbsp;&nbsp;年龄:"+request.getParameter("user_age")+"&nbsp;&nbsp;所在部门:"+request.getParameter("bumen"));

}

%>

</p>

</body>

</html>

截图: