如何使用servlet将文本文件中的数据显示到网页上? [英] How to display data from a text file onto a webpage using servlets?

查看:97
本文介绍了如何使用servlet将文本文件中的数据显示到网页上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,


我设法编写了一个java类,它打开并读取文本文件并将数据打印到网页上。但是为了做到这一点,我每次都要编译我的java类。有没有人知道如何使用servlet和jsp做同样的事情,我一直在努力做到,但还没有成功。

Hi Guys,

I have managed to write a java class which opens and reads a text file and prints the data onto a webpage. however in order to do this I have to compile my java class each time. Does any one know how I could do the same thing using servlets and jsp, I have been trying to do it, but have not succeeded yet.

推荐答案


大家好,


我设法编写了一个java类,它打开并读取文本文件并将数据打印到网页上。但是为了做到这一点,我每次都要编译我的java类。有没有人知道如何使用servlet和jsp做同样的事情,我一直试图这样做,但还没有成功。
Hi Guys,

I have managed to write a java class which opens and reads a text file and prints the data onto a webpage. however in order to do this I have to compile my java class each time. Does any one know how I could do the same thing using servlets and jsp, I have been trying to do it, but have not succeeded yet.



你是如何尝试设置JSP和servlet的?你在使用哪个服务器?请发布您尝试过的代码以及您遇到的错误。

How did you try to set up your JSP and servlet? Which server are you using? Please post the codes you have tried with and the error that you are getting.



您是如何尝试设置JSP和servlet的? ?你在使用哪个服务器?请发布您尝试过的代码以及您获得的错误。
How did you try to set up your JSP and servlet? Which server are you using? Please post the codes you have tried with and the error that you are getting.



我现在使用了一个servlet,我使用tomcat来运行它。


这是下面的代码:


第13行有一个错误:找不到符号变量文本。


import javax.servlet。*;

import javax.servlet.http。*;

import java.io. *;


公共类TestServlet扩展HttpServlet {


字符串valString;


public void init()抛出ServletException {


try {

BufferedReader dataFile = Text.open(" DataFile.txt");


valString = dataFile.readLine();


if(valString!= null)


System.out.println(" Data is:" + valString);


dataFile.close();

}


catch(FileNotFoundException e){

System.err.println (FileStreamsTest:+ e);


}

}

public void doPost(HttpServletRequest req,HttpServletResponse res)

抛出ServletException,IOException {


res.setContentType(" text / html");

PrintWriter out = new PrintWriter(res.getOutputStream());



out.println("< html>");

out.println("< head>< title> Temperature< / title>");

out.println("< / head>< body>");

out.println("< b> ;今天的温度是: + valString +"< / b>< br>");

out.println("< / body>< / html>");


out.close();

}



}

I used a servlet for now, im using tomcat to run it.

This is the code below:

There is an error on line 13:Can not find symbol variable text.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class TestServlet extends HttpServlet {

String valString;

public void init() throws ServletException{

try {

BufferedReader dataFile = Text.open("DataFile.txt");

valString = dataFile.readLine();

if(valString != null)

System.out.println(" Data is: " + valString);

dataFile.close();
}


catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);


}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = new PrintWriter(res.getOutputStream());


out.println("<html>");
out.println("<head><title>Temperature</title>");
out.println("</head><body>");
out.println("<b>The temperature today is: " + valString + "</b><br>");
out.println("</body></html>");

out.close();
}



}



我现在使用了一个servlet,我使用tomcat来运行它。


这是下面的代码: br />

第13行有一个错误:找不到符号变量文本。


import javax.servlet。*;

import javax.servlet.http。*;

import java.io. *;


公共类TestServlet扩展HttpServlet {


字符串valString;


public void init()抛出ServletException {


try {


BufferedReader dataFile = Text.open(" DataFile.txt");


valString = dataFile.readLine();


if(valString!= null)


System.out.printl N("数据是: + valString);


dataFile.close();

}


catch(FileNotFoundException) e){

System.err.println(" FileStreamsTest:" + e);



}

}

public void doPost(HttpServletRequest req,HttpServletResponse res)

抛出ServletException,IOException {


res.setContentType( " text / html");

PrintWriter out = new PrintWriter(res.getOutputStream());


out.println(" ;< html>");

out.println("< head>< title> Temperature< / title>");

out。 println("< / head>< body>");

out.println("< b>今天的温度是:" + valString +"< / b>< br>");

out.println("< / body>< / html>& ;);


out.close();

}




}
I used a servlet for now, im using tomcat to run it.

This is the code below:

There is an error on line 13:Can not find symbol variable text.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class TestServlet extends HttpServlet {

String valString;

public void init() throws ServletException{

try {

BufferedReader dataFile = Text.open("DataFile.txt");

valString = dataFile.readLine();

if(valString != null)

System.out.println(" Data is: " + valString);

dataFile.close();
}


catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);


}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = new PrintWriter(res.getOutputStream());


out.println("<html>");
out.println("<head><title>Temperature</title>");
out.println("</head><body>");
out.println("<b>The temperature today is: " + valString + "</b><br>");
out.println("</body></html>");

out.close();
}



}



1.)发布代码时使用代码标签。这样更容易阅读

2.)打开文件的代码也应该放在doPost方法中。使用init初始化servlet和环境参数。

3.)Text.open中的文本是什么...

你应该使用

1.) Use code tags when posting code. It''s easier to read that way
2.)The code for opening the file should be put in the doPost Method as well. Use the init for initializing servlet and environment parameters only.
3.)What is Text in Text.open ...
You should use

展开 | 选择 | Wrap | 行号


这篇关于如何使用servlet将文本文件中的数据显示到网页上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆