老师,帮我看一下哪块有错误?
来源:6-3 自由编程
慕先生8145006
2019-09-30 13:31:05
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "/WebDemo1/direct/rs">
<input type = "text" name = "" placeholder = "请输入要查询的单词">
<input type = "submit" value = "查询">
</form>
</body>
</html>
package com.imooc.servlet.direct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class RequestServlet
*/
@WebServlet("/direct/rs")
public class RequestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String word = request.getParameter("word");
Map<String,String> fruit = new HashMap();
fruit.put("apple","苹果");
fruit.put("orange","橘子");
fruit.put("banana","香蕉");
if(fruit.containsKey(word)) {
request.setAttribute("value", fruit.get(word));
request.getRequestDispatcher("success.jsp").forward(request,response);
}else {
HttpSession session = request.getSession();
session.setAttribute("str","没有找到对应的单词解释");
response.sendRedirect("/WebDemo1/fail.jsp");
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String value = (String)request.getParameter("value");
String html = "<h1 style=color:blue>" + value + "</h1>";
response.getWriter().println(html);
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String str = (String)request.getParameter("str");
String html = "<h1 style=color:red>" + str + "</h1>";
response.getWriter().println(html);
%>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>WebDemo1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
1回答
同学你好!
同学的后台使用word来获取数据,但是页面没有给input框的name赋值


建议同学在页面给name添加值

同学在返回成功页面时,注意在地址前面添加/,否则跳转的地址是当前路径下的页面,会报404,修改后的代码:

后台返回前端的不能通过request.getParameter()的这种方式来获取,或者
如果是转发通过request.getAttribute("")来获取,如果是重定向可以通过seesion.getAttribute("")来获取,比如:
成功的页面:

失败的页面:

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题