I'm a beginner at Servlet.
Today I'm studying servlet encoding.
I encountered a garbled character while creating a string from a byte string.
OS: fedora28
jdk —java10
server:tomcat9
IDE:eclipse 4.8
Here's the source code:
servlet:
package getBytes_fun;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestSample2 extensions HttpServlet{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse)
* response)
*/
protected void doGet (HttpServletRequest request, HttpServletResponse response)
US>throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html; charset=UTF-8";
response.getWriter().append("Served at:").append(request.getContextPath());
PrintWriter out=response.getWriter();
String tmp;
String name="";
tmp = request.getParameter("name");
if(tmp==null||tmp.length()==0){
name = "Unconfigured";
} else{
name = decodeString(tmp);
}
intold;
tmp = request.getParameter("old");
if(tmp==null||tmp.length()==0){
old = -1;
} else{
try{
old = Integer.parseInt(tmp);
} catch(NumberFormatExceptione){
// TODO:handle exception
old = -1;
}
}
String tmps[] = request.getParameterValues("food");
String food="";
if(tmps!=null){
for(inti=0;i<tmps.length;i++){
food+=decodeString(tmps[i]);
food+="";
}
} else{
food = "No";
}
StringBuffersb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>Sample</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<p>What's your name?");
sb.append(name);
sb.append("is </p>");
sb.append("<p>age is ");
if(old==-1){
sb.append("Not Configured </p>");
} else{
sb.append(old);
sb.append("is </p>");
}
sb.append("<p>Favorite fruit is");
sb.append(food);
sb.append("is </p>");
sb.append("</body>");
sb.append("</html>");
out.println(new String(sb));
out.close();
}
protected String decodeString(String str) {
// TODO Auto-generated method stub
try{
byte[] byteData=str.getBytes("ISO_8859_1");
str = new String (byteData, "UTF-8");
} catch(Exceptione){
// TODO:handle exception
return null;
}
return str;
}
}
html file:
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>Questionnaire Survey</p>
<form action="/ERP/RequestSample2" method="get">
<table>
<tr>
<td>Name</td>
<td><input type="text" size="20" value="name="name"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" size="5" value="name="old"></td>
</tr>
<tr>
<td>Favorite Fruits</td>
<td><select name="food" size="3" multiple>
<option value="Apple">Apple</option>
<option value="melon">melon</option>
<option value="wine">wine</option>
</select></td>
</tr>
</table>
<input type="submit" name="button1" value="send">
</form>
Printout Results:
Please let me know.
java html eclipse tomcat servlet
Problem with decodeString().
Once you delete decodeString(), you can view it without any problems.
Resolution.
© 2024 OneMinuteCode. All rights reserved.