String food[]=request.getParameterValues("food");
时间: 2024-05-24 17:10:56 浏览: 10
This line of code is used in Java to retrieve multiple values submitted through an HTML form element with the name "food". The "getParameterValues()" method is called on the "request" object to retrieve an array of strings containing all the values submitted with the name "food".
For example, if a user submitted a form with checkboxes for different food items like pizza, burgers, and tacos, and selected all three, this code would retrieve an array with three elements: ["pizza", "burgers", "tacos"].
The retrieved values can then be used for further processing, such as storing them in a database or displaying them on a webpage.
相关问题
<% 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); %>哪里错了
这段代码中可能会出现一些问题,具体如下:
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);
}
%>
```
public void save() { String title = request.getParameter("title"); String pubren = request.getParameter("pubren"); String pubtime = request.getParameter("pubtime"); String laiyuan=request.getParameter("laiyuan"); String dcontent = request.getParameter("dcontent"); String tupian2 = request.getParameter("tupian2"); String lanmuid = request.getParameter("lanmuid"); String lanmuming = request.getParameter("lanmuming"); String tuijian=request.getParameter("tuijian"); String hot=request.getParameter("hot"); String zuixin=request.getParameter("zuixin"); String zhaiyao=request.getParameter("zhaiyao"); String style=request.getParameter("style"); SimpleDateFormat sdfxinxi = new SimpleDateFormat("yyyy-MM-dd"); Xinxi xinxi = new Xinxi(); xinxi.setTitle(title == null ? "" : title); xinxi.setPubren(pubren == null ? "" : pubren); xinxi.setPubtime(new Date()); xinxi.setHot(hot!=null?1:0); xinxi.setTuijian(tuijian!=null?1:0); xinxi.setZuixin(zuixin!=null?1:0); xinxi.setClickcount(0); xinxi.setZhaiyao(zhaiyao==null?"":zhaiyao); xinxi.setDcontent(dcontent == null ? "" : dcontent); xinxi.setTupian2(tupian2 == null ? "" : tupian2); xinxi.setAgainstcount(0); xinxi.setAgreecount(0); xinxi.setLanmuid(new Integer(lanmuid)); xinxi.setLanmuming(lanmuming == null ? "" : lanmuming); DALBase.save(xinxi); try { if(style!=null&&style.equals("admin")) response.sendRedirect("xinximanager.do?actiontype=get&seedid=201&lanmuid="+xinxi.getLanmuid()); if(style!=null&&style.equals("huiyuan")) response.sendRedirect("../e/myxinximanager.jsp?seedid=m2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /****************************************************** *********************** 内部附件支持********************* ***public void attachements(HttpServletRequest request, HttpServletResponse response, String belongid) { DALBase.delete("attachement", MessageFormat.format( " where belongid=''{0}'' and belongtable=''xinxi'' ", belongid)); String[] photos = request.getParameterValues("fileuploaded"); if (photos == null) return; for (int i = 0; i < photos.length; i++) { Attachement a = new Attachement(); a.setType("images"); a.setPubtime(new Date()); a.setBelongfileldname("id"); a.setFilename(photos[i]); a.setBelongid(belongid); a.setBelongtable("xinxi"); a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/" + a.getFilename()); a.setTitle(a.getFilename()); DALBase.save(a); } }解释每一行代码
这是一段Java代码,根据传入的HTTP请求参数保存一个信息(Xinxi)到数据库中。下面是每行代码的解释:
```java
public void save() { // 定义一个公有方法save()
// 从请求参数中获取信息标题、发布者、发布时间、来源、正文内容、图片、栏目ID、栏目名称、推荐、热点和最新等信息
String title = request.getParameter("title");
String pubren = request.getParameter("pubren");
String pubtime = request.getParameter("pubtime");
String laiyuan=request.getParameter("laiyuan");
String dcontent = request.getParameter("dcontent");
String tupian2 = request.getParameter("tupian2");
String lanmuid = request.getParameter("lanmuid");
String lanmuming = request.getParameter("lanmuming");
String tuijian=request.getParameter("tuijian");
String hot=request.getParameter("hot");
String zuixin=request.getParameter("zuixin");
String zhaiyao=request.getParameter("zhaiyao");
String style=request.getParameter("style");
// 定义一个日期格式化对象,格式为"yyyy-MM-dd"
SimpleDateFormat sdfxinxi = new SimpleDateFormat("yyyy-MM-dd");
// 创建一个信息对象
Xinxi xinxi = new Xinxi();
// 将获取到的信息设置到信息对象中
xinxi.setTitle(title == null ? "" : title);
xinxi.setPubren(pubren == null ? "" : pubren);
xinxi.setPubtime(new Date()); // 设置发布时间为当前时间
xinxi.setHot(hot!=null?1:0); // 如果参数hot不为空,则将热点属性设为1,否则设为0
xinxi.setTuijian(tuijian!=null?1:0); // 如果参数tuijian不为空,则将推荐属性设为1,否则设为0
xinxi.setZuixin(zuixin!=null?1:0); // 如果参数zuixin不为空,则将最新属性设为1,否则设为0
xinxi.setClickcount(0); // 设置点击次数为0
xinxi.setZhaiyao(zhaiyao==null?"":zhaiyao);
xinxi.setDcontent(dcontent == null ? "" : dcontent);
xinxi.setTupian2(tupian2 == null ? "" : tupian2);
xinxi.setAgainstcount(0); // 设置反对次数为0
xinxi.setAgreecount(0); // 设置赞同次数为0
xinxi.setLanmuid(new Integer(lanmuid)); // 将栏目ID设置到信息对象中
xinxi.setLanmuming(lanmuming == null ? "" : lanmuming);
// 调用数据访问层的save()方法保存信息对象到数据库中
DALBase.save(xinxi);
try {
// 根据请求参数中的style值进行不同的重定向操作
if(style!=null&&style.equals("admin"))
response.sendRedirect("xinximanager.do?actiontype=get&seedid=201&lanmuid="+xinxi.getLanmuid());
if(style!=null&&style.equals("huiyuan"))
response.sendRedirect("../e/myxinximanager.jsp?seedid=m2");
} catch (IOException e) {
e.printStackTrace();
}
}
// 定义一个公有方法attachements()用于支持内部附件
public void attachements(HttpServletRequest request, HttpServletResponse response, String belongid) {
// 删除已存在的附件
DALBase.delete("attachement", MessageFormat.format(" where belongid=''{0}'' and belongtable=''xinxi'' ", belongid));
// 获取请求参数中的上传的图片文件名
String[] photos = request.getParameterValues("fileuploaded");
if (photos == null)
return;
// 遍历图片文件名数组,创建附件对象并保存到数据库中
for (int i = 0; i < photos.length; i++) {
Attachement a = new Attachement();
a.setType("images");
a.setPubtime(new Date()); // 设置附件发布时间为当前时间
a.setBelongfileldname("id");
a.setFilename(photos[i]);
a.setBelongid(belongid);
a.setBelongtable("xinxi");
a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/" + a.getFilename());
a.setTitle(a.getFilename());
DALBase.save(a);
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![sql](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)