<% String name=request.getParameter("name"); name=new String(name.getBytes("iso-8859-1"),"UTF-8"); session.setAttribute("name",name); String sex=request.getParameter("gender"); String[] hobby=request.getParameterValues("hobby"); String mes=""; if(name.equals("")){ mes="昵称不能为空,请输入昵称"; mes = URLEncoder.encode(mes); session.setAttribute("mes",mes); RequestDispatcher rd = request.getRequestDispatcher("input.jsp"); rd.forward(request,response); } else{ RequestDispatcher rd = request.getRequestDispatcher("infromation.jsp"); rd.forward(request,response); } session.setAttribute("sex",sex); session.setAttribute("hobby",hobby); %>哪里错了
时间: 2023-07-19 17:09:19 浏览: 112
这段代码中可能会出现一些问题,具体如下:
1. 在设置 name 属性时,应该先进行非空判断,否则可能会出现空指针异常。
2. 在进行转码时,应该使用 try-catch 块进行异常处理。
3. 在设置完 name 属性后,应该立即跳转到相应的页面,否则可能会出现逻辑错误。
建议修改后的代码如下:
```
<%
String name = request.getParameter("name");
String sex = request.getParameter("gender");
String[] hobby = request.getParameterValues("hobby");
String mes = "";
if (name == null || name.equals("")) {
mes = "昵称不能为空,请输入昵称";
mes = URLEncoder.encode(mes, "UTF-8");
session.setAttribute("mes", mes);
RequestDispatcher rd = request.getRequestDispatcher("input.jsp");
rd.forward(request, response);
} else {
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
session.setAttribute("name", name);
session.setAttribute("sex", sex);
session.setAttribute("hobby", hobby);
RequestDispatcher rd = request.getRequestDispatcher("information.jsp");
rd.forward(request, response);
}
%>
```
阅读全文
相关推荐





String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8"); System.out.print(name); if (name == null || name.isEmpty()) { %> 请输入中药名称,点击返回重新输入 <% }else{ Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = SQLConn.openDB(); stmt = conn.createStatement(); // 执行查询等操作 String sql = "select ZYName,YLName,YDY,YX,GX,ZhongYao.ZZ as ZZ from Yaolei,ZhongYao where Yaolei.YLID=ZhongYao.YLID"; PreparedStatement pstmt = conn.prepareStatement(sql); //pstmt.setString(1, "%" + name + "%"); rs = pstmt.executeQuery(); int count = 0; if(rs.next()){ while (rs.next()) { String ZYName = rs.getString("ZYName"); System.out.print(name); if (name.trim().equals(ZYName.trim())) { // if (name.trim().equals(A.trim())) { %>为什么name.trim().equals(ZYName.trim())不成立

String name = new String(request.getParameter("ZY").getBytes("ISO-8859-1"), "UTF-8"); //System.out.print(name); if (name == null || name.isEmpty()) { %> 请输入中药名称,点击返回重新输入 <% }else{ Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = SQLConn.openDB(); stmt = conn.createStatement(); String sql = "select ZYName,YLName,YDY,YX,GX,ZhongYao.ZZ as ZZ from Yaolei,ZhongYao where Yaolei.YLID=ZhongYao.YLID and ZYName like ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "'%" + name + "%'"); rs = pstmt.executeQuery(); int count = 0; if(rs.next()){输入正确的数据,为什么rs.next()为空












