编写jsp程序
使用JSP编程实现星座查询
简介:
在使用JSP编程实现星座查询功能时,我们可以通过输入用户的出生日期,根据星座计算规则判断用户所属的星座,并返回相应的结果。以下是一个示例代码,展示了如何使用JSP编程实现星座查询功能。
代码示例:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF8"
pageEncoding="UTF8"%>
星座查询
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<% 定义星座计算规则 %>
<%
Map
constellationMap.put("魔羯座", "12220119");
constellationMap.put("水瓶座", "01200218");
constellationMap.put("双鱼座", "02190320");
constellationMap.put("白羊座", "03210419");
constellationMap.put("金牛座", "04200520");
constellationMap.put("双子座", "05210620");
constellationMap.put("巨蟹座", "06210722");
constellationMap.put("狮子座", "07230822");
constellationMap.put("处女座", "08230922");
constellationMap.put("天秤座", "09231022");
constellationMap.put("天蝎座", "10231121");
constellationMap.put("射手座", "11221221");
%>
<% 获取用户输入的出生日期 %>
<%
String birthdateStr = request.getParameter("birthdate");
if (birthdateStr != null && !birthdateStr.isEmpty()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date birthdate = sdf.parse(birthdateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(birthdate);
int month = calendar.get(Calendar.MONTH) 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
// 获取星座
String constellation = "";
for (Map.Entry
String[] dates = entry.getValue().split("");
int startMonth = Integer.parseInt(dates[0]);
int startDay = Integer.parseInt(dates[1]);
int endMonth = Integer.parseInt(dates[2]);
int endDay = Integer.parseInt(dates[3]);
if ((month == startMonth && day >= startDay) || (month == endMonth && day <= endDay)) {
constellation = entry.getKey();
break;
}
}
// 显示结果
out.println("
您的星座是:" constellation "
");}
%>