できるよ。おそらく基礎ができていない。
index.jspで以下を記述。
<%@page import="java.net.URLEncoder"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<a href="/ExerciseApplication/Test?myans=<%=URLEncoder.encode("ア","UTF-8")%>&toi">ア</a>
<a href="/ExerciseApplication/Test?myans=<%=URLEncoder.encode("イ","UTF-8")%>&toi">イ</a>
<a href="/ExerciseApplication/Test?myans=<%=URLEncoder.encode("ウ","UTF-8")%>&toi">ウ</a>
<a href="/ExerciseApplication/Test?myans=<%=URLEncoder.encode("エ","UTF-8")%>&toi">エ</a>
<a href="/ExerciseApplication/Test?myans=<%=URLEncoder.encode("オ","UTF-8")%>&toi">オ</a>
</body>
</html>
TestServlet.javaで以下を記述
package test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "TestServlet", urlPatterns = {"/Test"})
public class TestServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String query = request.getParameter("myans");
System.out.println(query); // コンソールに出力
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
EncordingFilter.javaで以下を記述
package common;
import java.io.IOException;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
@WebFilter(
urlPatterns = {"/*"},
initParams = {
@WebInitParam(name = "encoding", value = "UTF-8")
},
dispatcherTypes = {
DispatcherType.FORWARD,
DispatcherType.REQUEST,
DispatcherType.INCLUDE
}
)
public class EncodingFilter implements Filter{
private String encoding = null;
@Override
public void destroy() { }
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
req.setCharacterEncoding(this.encoding);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
this.encoding = config.getInitParameter("encoding");
}
}