Java 试图在应用程序和servlet之间连接时,URLConnection.getInputStream()会引发FileNotFoundException
我想开发一个在Tomcat上运行的servlet文件之间进行通信的应用程序 下面是我的应用程序中的代码,它试图连接到此servlet,发送请求并获取响应Java 试图在应用程序和servlet之间连接时,URLConnection.getInputStream()会引发FileNotFoundException,java,tomcat,servlets,Java,Tomcat,Servlets,我想开发一个在Tomcat上运行的servlet文件之间进行通信的应用程序 下面是我的应用程序中的代码,它试图连接到此servlet,发送请求并获取响应 private URLConnection getServletConnection() { try { URL servletURL = new URL("http://localhost:8080/test/servlet"); URLConnection conn = servletURL.ope
private URLConnection getServletConnection() {
try {
URL servletURL = new URL("http://localhost:8080/test/servlet");
URLConnection conn = servletURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
return conn;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
下面是引发异常的代码:
URLConnection conn = getServletConnection();
OutputStream outputStream = conn.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
ServletRequestMessage srm = new ServletRequestMessage(2, username, password);
oos.writeObject(srm);
oos.flush();
oos.close();
InputStream inputStream = conn.getInputStream();
例外情况是:
java.io.FileNotFoundException: http://localhost:8080/test/servlet
有人能帮我吗?谢谢。服务器端
public class ServletImpl extends HttpServlet implements Servlet {
....
public ServletImpl() {
super();
}
public void init(ServletConfig config) throws ServletException{
super.init(config);
/*
Application scope
Shared between all servlets, JSP pages, and custom tags within a J2EE application
or within the whole container if no applications are defined.
The programmatic interface to the application scope is the 'ServletContext' object.
*/
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
/* where "base" is iniy param in web.xml
<init-param>
<param-name>base</param-name>
<param-value>/ServlrtName/sys</param-value>
</init-param>
*/
....
}
....
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
Enumeration<?> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if("sub".equals(paramName)){
paramValues = request.getParameterValues(paramName);
if(paramValues.length > 0){
String param = paramValues[0];
// do something
....
}
}
}
}
....
// prepear response
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(mMessageResponseStr);
out.close();
客户端
public class ServletImpl extends HttpServlet implements Servlet {
....
public ServletImpl() {
super();
}
public void init(ServletConfig config) throws ServletException{
super.init(config);
/*
Application scope
Shared between all servlets, JSP pages, and custom tags within a J2EE application
or within the whole container if no applications are defined.
The programmatic interface to the application scope is the 'ServletContext' object.
*/
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
/* where "base" is iniy param in web.xml
<init-param>
<param-name>base</param-name>
<param-value>/ServlrtName/sys</param-value>
</init-param>
*/
....
}
....
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
Enumeration<?> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if("sub".equals(paramName)){
paramValues = request.getParameterValues(paramName);
if(paramValues.length > 0){
String param = paramValues[0];
// do something
....
}
}
}
}
....
// prepear response
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(mMessageResponseStr);
out.close();
我使用了DefaultHttpClient
和HttpPost
。我发送sub
标签。下面是一个将数据发送到Servlet的方法:
public boolean send(String data) {
DefaultHttpClient httpclient = null;
boolean success = false;
try {
httpclient = new DefaultHttpClient();
String url = "your URL";
HttpPost httpost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("sub", data));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
if (entity != null) {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200){
mResErr.onErrorResponse(statusCode);
}
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) {
// get data from line
}
is.close();
} else {
//response is null/
}
success = true;
mRes.onHttpResponse(mArr);
} catch (Exception e) {
mResErr.onErrorResponse(e);
e.getStackTrace();
}
if (httpclient != null) {
// resource cleanup
httpclient.getConnectionManager().shutdown();
}
return success;
}
服务器端
public class ServletImpl extends HttpServlet implements Servlet {
....
public ServletImpl() {
super();
}
public void init(ServletConfig config) throws ServletException{
super.init(config);
/*
Application scope
Shared between all servlets, JSP pages, and custom tags within a J2EE application
or within the whole container if no applications are defined.
The programmatic interface to the application scope is the 'ServletContext' object.
*/
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
/* where "base" is iniy param in web.xml
<init-param>
<param-name>base</param-name>
<param-value>/ServlrtName/sys</param-value>
</init-param>
*/
....
}
....
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
Enumeration<?> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if("sub".equals(paramName)){
paramValues = request.getParameterValues(paramName);
if(paramValues.length > 0){
String param = paramValues[0];
// do something
....
}
}
}
}
....
// prepear response
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(mMessageResponseStr);
out.close();
客户端
public class ServletImpl extends HttpServlet implements Servlet {
....
public ServletImpl() {
super();
}
public void init(ServletConfig config) throws ServletException{
super.init(config);
/*
Application scope
Shared between all servlets, JSP pages, and custom tags within a J2EE application
or within the whole container if no applications are defined.
The programmatic interface to the application scope is the 'ServletContext' object.
*/
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
/* where "base" is iniy param in web.xml
<init-param>
<param-name>base</param-name>
<param-value>/ServlrtName/sys</param-value>
</init-param>
*/
....
}
....
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
Enumeration<?> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if("sub".equals(paramName)){
paramValues = request.getParameterValues(paramName);
if(paramValues.length > 0){
String param = paramValues[0];
// do something
....
}
}
}
}
....
// prepear response
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(mMessageResponseStr);
out.close();
我使用了DefaultHttpClient
和HttpPost
。我发送sub
标签。下面是一个将数据发送到Servlet的方法:
public boolean send(String data) {
DefaultHttpClient httpclient = null;
boolean success = false;
try {
httpclient = new DefaultHttpClient();
String url = "your URL";
HttpPost httpost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("sub", data));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
if (entity != null) {
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200){
mResErr.onErrorResponse(statusCode);
}
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) {
// get data from line
}
is.close();
} else {
//response is null/
}
success = true;
mRes.onHttpResponse(mArr);
} catch (Exception e) {
mResErr.onErrorResponse(e);
e.getStackTrace();
}
if (httpclient != null) {
// resource cleanup
httpclient.getConnectionManager().shutdown();
}
return success;
}
您能否指定抛出
FileNotFoundException
的确切位置?除了@Andrew之外,您是否验证了http://localhost:8080/test/servlet
该url工作正常?@AndrewLogvinov'InputStream InputStream=conn.getInputStream()'引发异常。有趣的是,前面的代码“OutputStream OutputStream=conn.getOutputStream()”没有t@Fess是的,它在浏览器中工作。@缓存:Changeconn.setDoOutput(true)代码>至连接设置输出(假)代码>并查看。我认为这一行正在更改请求方法。您能否指定抛出FileNotFoundException
的确切位置?除了@Andrew之外,您是否验证了http://localhost:8080/test/servlet
该url工作正常?@AndrewLogvinov'InputStream InputStream=conn.getInputStream()'引发异常。有趣的是,前面的代码“OutputStream OutputStream=conn.getOutputStream()”没有t@Fess是的,它在浏览器中工作。@缓存:Changeconn.setDoOutput(true)代码>至连接设置输出(假)代码>并查看。我认为这一行正在改变请求方式。非常感谢您的帮助。但是我对你的web.xml有一个问题:你的ServletImpl.class文件在哪里?它在webapps/com/demo/servlet下吗?是的,`com.demo.servlet.ServletImpl`。从客户端获取我的示例URL:那么您需要更改server.xml文件吗?一点也不需要,您在Servlet项目中的。/WebContent/web-INF/
下仅使用web.xml,如以下链接所示:非常感谢您的帮助。但是我对你的web.xml有一个问题:你的ServletImpl.class文件在哪里?它在webapps/com/demo/servlet下吗?是的,`com.demo.servlet.ServletImpl`。从客户端获取我的示例URL:那么您需要更改server.xml文件吗?一点也不需要,您在Servlet项目中的。/WebContent/web-INF/
下只使用web.xml,如下链接所示: