gpt4 book ai didi

java - 使用 JSP 和 Servlet 显示当前日期

转载 作者:行者123 更新时间:2023-12-01 13:10:34 25 4
gpt4 key购买 nike

我正在尝试做有关 Web 编程(JSP 和 Servlet)的作业,但我看不出代码中存在什么问题。

我的index.jsp是:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Data (sem JavaBeans)</title>
</head>
<body>

<a href="Data">Atualizar data</a>
<br/>

<%

if (request.getAttribute("dia") != null)
{
int dia = (Integer) request.getAttribute("dia");
int mes = (Integer) request.getAttribute("mes");
int ano = (Integer) request.getAttribute("ano");
int hora = (Integer) request.getAttribute("hora");
int minuto = (Integer) request.getAttribute("minuto");

String sDia = String.format("%2d",Integer.toString(dia));
String sMes = String.format("%2d",Integer.toString(mes));
String sAno = String.format("%4d",Integer.toString(ano));
String sHora = String.format("%2d",Integer.toString(hora));
String sMinuto = String.format("%2d",Integer.toString(minuto));

out.print("A hora atual é: " + sDia + "/" + sMes + "/" + sAno + " - " + sHora + ":" + sMinuto);
}
%>
</body>
</html>

我的 servlet 是:

package br.com.gabriel;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Data
*/
@WebServlet("/Data")
public class Data extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Data() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("deprecation")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Date d = new Date();

int dia = d.getDate();
int mes = d.getMonth() + 1;
int ano = d.getYear() + 1900;
int hora = d.getHours();
int minuto = d.getMinutes();

request.setAttribute("dia", dia);
request.setAttribute("mes", mes);
request.setAttribute("ano", ano);
request.setAttribute("hora", hora);
request.setAttribute("minuto", minuto);

request.getRequestDispatcher("index.jsp").forward(request, response);
};
}

我收到的错误是:

java.util.IllegalFormatConversionException: d != java.lang.String

但是我看不出这里错在哪里......

提前致谢!加布里埃尔

最佳答案

在这样的行中

String sDia = String.format("%2d",Integer.toString(dia));

摆脱Integer.toString(..)调用。中使用的 d 模式

%2d

需要一个整数值,但您传递的是一个字符串

简单地做

String sDia = String.format("%2d", dia);

关于java - 使用 JSP 和 Servlet 显示当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com