I've created a site that displays the weather of every city using an API from openweather.
I managed to solve a problem I had where the previous searches wouldn't show up using cookies. This is for a school project that requires that I use cookies.
My problem now is that I can't create whitespace between the previous searches. I keep getting the error.
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
The only characters that can be used are colon and hyphen as far as the ones I've tried. I read that colon and whitespace used to be accepted on an earlier tomcat, but not anymore. I'm on 9.0.69.
I've uploaded a picture, so you can see the end result. How do I create a white space between the cities or better yet a list that goes downwards.
This is my servlet;
package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.GettheWeather;
import model.weatherBean;
@WebServlet("/OWservlet")
public class OWservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cityStr = request.getParameter("city");
String countryStr = request.getParameter("country");
weatherBean wBean = new weatherBean(cityStr, countryStr);
GettheWeather.getWeather(wBean);
request.setAttribute("wBean", wBean);
Cookie[] cookies = request.getCookies();
String temp = "";
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("city")) {
temp = cookies[i].getValue();
}
}
temp = temp + "-" + cityStr;
Cookie ck = new Cookie("city", temp);
ck.setMaxAge(100);
response.addCookie(ck);
RequestDispatcher rd = request.getRequestDispatcher("views/showWeather.jsp");
rd.forward(request, response);
}
}
and this is my JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="model.weatherBean"%>
<%@page import="controller.OWservlet"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>the weather</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<%
weatherBean wBean = (weatherBean) request.getAttribute("wBean");
double number = Double.parseDouble(wBean.getTempStr());
number = number - 273.15;
int number2 = (int) number;
out.print("<h2>The weather in " + wBean.getCityStr() + " is now " + wBean.getCloudsStr() + ", the temperature is : "
+ number2 + "°C, and the date is: " + wBean.getTimeStr().substring(0, 10) + "</h2>");
%>
<div class="loginbox">
<form action="OWservlet" method="get">
City:<input type="text" name="city" /><br /> Country:<input
type="text" name="country" /><br /> <input type="submit"
value="go" />
</form>
</div>
<h1>Previously searched cities: ${cookie["city"].getValue()}</h1>
</body>
</html>
I've tried using
temp.split("-");
and
temp.replace("-"," ")