老师看一下我的代码 怎么提交后是个下载页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Text/check">
<input type="text" name="key" placeholder="请输入要查询的单词">
<button type="submit">查询</button>
</form>
</body>
</html>
====================================================================
<servlet>
<servlet-name>name</servlet-name>
<servlet-class>com.imooc.text.CheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
================================================================
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String> map=new HashMap<String,String>();
map.put("apple", "苹果");
map.put("banana", "香蕉");
map.put("orange", "橙子");
String danci=request.getParameter("key");
boolean flag= map.containsKey(danci);
if(flag) {
request.setAttribute("key", map.get(danci));
request.getRequestDispatcher("/Success.jsp").forward(request, response);
}else {
HttpSession session=request.getSession();
String nof="没有找到对应的单词解释";
session.setAttribute("error", nof);
response.sendRedirect("/Text/fail.jsp");
}
============================================================
<%
String keys=(String)request.getAttribute("key");
response.setContentType("html/text;charset=utf-8");
response.getWriter().println(keys);
%>
====================================================
<%
String str=(String)session.getAttribute("error");
out.println("<h1>"+str+"</h1>");
%>
分别为html文件、Servlet代码、两个jsp文件
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星