错误提示java.lang.StringIndexOutOfBoundsException: String index out of range: -8 java.lang.String.substring
给出一部分代码:
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//取消乱码了
//String s=new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
//System.out.println(s);
String url = request.getRequestURI();
String action = url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));
//HttpSession session1 = request.getSession();
User user=new User();
UserDao userDao= new UserDao();
DeleteUserDao deleteUserDao=new DeleteUserDao();
FindDao findDao =new FindDao();
FindByNameDao findByNameDao=new FindByNameDao();
UpdateDao updateDao=new UpdateDao();
//增加用户
if("/add".equals(action)){
//user.setId(Integer.parseInt(request.getParameter("id")));
user.setUserName(new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8"));
user.setPwd(new String(request.getParameter("pwd").getBytes("iso-8859-1"),"utf-8"));
user.setAge(Integer.parseInt(request.getParameter("userAge")));
user.setSex(new String(request.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8"));
user.setHobby(new String(request.getParameter("hobby").getBytes("ISO-8859-1"),"UTF-8"));
user.setEdu(new String(request.getParameter("edu").getBytes("ISO-8859-1"),"UTF-8"));
那应该怎么解决
追答你都采纳别人了,还问我……
在前面增加判断咯,先判断是否符合格式之类的
或者,判断两个 lastIndexOf 是否为 >= 0, 也行