简体   繁体   中英

How to run socket server/client from a servlet to give better access on project code and files to Tomcat Server?

(Please Read Update #3 to understand where exactly my problem is now) I have written a custom SQL program that gets user commands from a GUI client and sends them to server(which contains the Database) to process all using Sockets.

Now I'm trying to write a web client for this database using apache tomcat(and JSP).I have put all the java files from the socket version to the src also I've learned how to use JSP files so now the user can login from a web page and the program will check the login information pretty much the way it did with the previous server/client but without socket while running tomcat.

At this point now I have made a page with a textarea and a submit button which will run the servlet code below upon clicking submit button:

@WebServlet("/change")
public class SQLCommandSender extends HttpServlet {

    DataStorage ds = new DataStorage();
    QueryParser parser = new QueryParser();
    Query q;
    QueryResult res;
    String textAreaValue;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            textAreaValue = request.getParameter("inConsole");
            q = parser.parse(textAreaValue);
            res = ds.execute(q);
            System.out.println(res.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And now this is what goes wrong; when I submit a create table command from the textarea the command processing reaches a point which a new txt file will be created for the new table:

  File tablez = new File("/Users/Sam/Documents/IntelliJ Projects/Login Database/C/DB/Tables/" + table.getName() + ".txt");

From what I found from debugging, when I run the exact same create command from my server socket code. this line runs without any problems and table is created without any error, but when I run the command from tomcat; the line above throws the following exceptions from up to down:

java.io.IOException: No such file or directory
java.io.FileNotFoundException: null/cat1$was.txt (No such file or directory)
java.lang.NullPointerException

Why something like this would happen ?

I should emphasis that both programs are accessing the same line of code in the same file.and the chunk of code that receives the raw command String and returns the log is the lines in try section of doPost method above.and same lines of code is used in socket version to execute commands.

UPDATE: Here 's the complete stack trace

UPDATE2: I have found this questions which looks somehow like my problem, but I still can't understand it properly It might help others to have some insight

UPDATE 3: from the latest info I have the problem probably stems from tomcat server's inability to access files the way a socket server/client can manipulate files. So now I'm trying to run socket server/client from previous phase of the program by Servlet .so if you can guide me on how to run a server/client with codes in a servlet(which was done manually before by right-click and clicking run button before). I will edit the question to match the new details after I've solved and seen your answers.

ps I'm using Intellij and tomcat is run with Intellij's plugin in form of a java EE web application. Also I have checked and the directory is correct.As I said the exact line works with previous server/client.

Tomcat Server threw an exception Running the same line that Socket Server runs correctly because it doesn't have the same direct access to the files in the src folder as a Socket Server/Client does.

I solved this problem by making a socket client in servlet and a socket Server in the DB and sending the command by that client instead of writing servlet codes like the ones in the question that directly made the server run the database codes without any mediator like socket Server which made problems as tomcat has restricted access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM