无法访问-提示localhost拒绝连接请求
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="servlet-advanced/ns">
<input type="text" name="num" placeholder="请输入100以内的数字">
<input type="submit" value="求累加和">
</form>
</body>
</html>
package com.imooc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class numberSum
*/
@WebServlet("/ns")
public class numberSum extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public numberSum() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String num=request.getParameter("num");
int sum=0;
for(int i=1;i<=Integer.parseInt(num);i++) {
sum+=i;
}
request.setAttribute("result", sum);
request.getRequestDispatcher("/nr").forward(request, response);
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class numResult
*/
@WebServlet("/nr")
public class numResult extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public numResult() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String num=request.getParameter("num");
String result=request.getAttribute("result").toString();
PrintWriter out=response.getWriter();
out.println("<h1>"+num+"累加和为:"+result+"<h1/>");
}
}
正在回答 回答被采纳积分+1
同学你好,1、首先贴出的代码运行没有问题,这里老师复制运行贴出代码,例如
2、这里提示-->无法访问-提示localhost拒绝连接请求,是tomcat的服务器没有配置成功,这里请问同学的端口号配置好了吗,服务器端口和浏览器中的端口号一致吗,比如(这里是老师复制同学代码的项目)
在服务器中的端口号为
对应在浏览器中
对应的修改端口,同学在sever中双击tomcat,进入tomcat设置中,如:
选择修改对应端口,修改端口也会在后面的章节中讲的哦!
3、 localhost单词是否写错? localhost表示本地地址,不能写错!
建议同学加上浏览器中url的访问地址,以及浏览器的访问界面的截图!便于老师的定位错误!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星