老师帮我看看代码的问题
package com.imooc.sumCalculator; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class sumCalculator extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] n=request.getParameterValues("num"); int sum=0,i=0; for(String num:n) { i=Integer.parseInt(num); sum+=i; } PrintWriter out=response.getWriter(); out.println("<div>加法计算器:</div>"); out.println("<div>运算结果为:"+sum+"</div>"); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="http://localhost:8080/ServletProj/sumCalculator"> <h1>加法计算器</h1><br/> 加数1:<input name="num" type="text"><br/> 加数2:<input name="num" type="text"><br/> <input type="submit" value="计算"><br/> </form> </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_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>ServletProj</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> <servlet> <servlet-name>zimu</servlet-name> <servlet-class>ServletProj</servlet-class> </servlet> <servlet-mapping> <servlet-name>zimu</servlet-name> <url-pattern>/ComputerServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>su</servlet-name> <servlet-class>com.imooc.sumCalculator</servlet-class> </servlet> <servlet-mapping> <servlet-name>su</servlet-name> <url-pattern>/sumCalculator</url-pattern> </servlet-mapping> </web-app>
1
收起
正在回答
2回答
<servlet-class>中同学只写到了包,没有写到类,改为如下代码试试:
<servlet>
<servlet-name>su</servlet-name>
<servlet-class>com.imooc.sumCalculator.sumCalculator</servlet-class>
</servlet>
另外,类名首字母注意大写呦。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星