0

The code was running well before till i included the code of reading a text file in my servlet program. Actually i need to read the text file and using regex pattern match have to search for pattern in the text file. I tried a lot but i cant figure out. Every time i run it using localhost it directly goes into catch and before i put try-catch it was showing error:500 file not found "database.txt" whereas my database.txt is in the same place as my servlet program webapps/myprogram/database.txt. I am using tomcat8. Please help me why it is not getting the file and entering the catch..

Here is my servlet program:

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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import java.io.*;

/**
 * @author hp
 */
@WebServlet(urlPatterns = {"/servlet1"})
public class servlet1 extends HttpServlet {


    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        String txt = request.getParameter("query");
        try {

            FileInputStream fstream = new FileInputStream("database.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            if (txt.matches("Who is the(.*)")) {
                String re1 = ".*?";
                String re2 = "(?:[a-z][a-z]+)";
                String re3 = ".*?";
                String re4 = "(?:[a-z][a-z]+)";
                String re5 = ".*?";
                String re6 = "(?:[a-z][a-z]+)";
                String re7 = ".*?";
                String re8 = "((?:[a-z][a-z]+))";
                String re9 = ".*?";
                String re10 = "(?:[a-z][a-z]+)";
                String re11 = ".*?";
                String re12 = "((?:[a-z][a-z]+))";

                Pattern p =
                        Pattern.compile(re1 + re2 + re3 + re4 + re5 + re6 + re7 + re8 + re9 + re10 + re11 + re12, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

                Matcher m = p.matcher(txt);
                if (m.find()) {
                    String word1 = m.group(1);
                    String word2 = m.group(2);
                    String z = word2.toString() + "&&" + word1.toString() + "&&" + "name.*" + "\n";

                    while ((strLine = br.readLine()) != null) {
                        if (Pattern.matches(z, strLine)) {
                            Pattern pattern = Pattern.compile("'(.*?)'");
                            Matcher matcher = pattern.matcher(strLine);
                            if (matcher.find()) {
                                String a = matcher.group(1);
                                request.setAttribute("a", a);
                                request.getRequestDispatcher("/output.jsp").forward(request, response);
                            }
                        }
                    }
                }
            } else {
                String z = "Sorry......Invalid query !!!";
                request.setAttribute("z", z);
                request.getRequestDispatcher("/output.jsp").forward(request, response);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        doGet(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
Karthikeyan Vaithilingam
  • 6,473
  • 10
  • 41
  • 60
Soumyasree Biswas
  • 41
  • 1
  • 2
  • 10
  • I think most people here would appreciate if you formatted your code for better readability and tried to focus on the problem. – jan groth Mar 23 '15 at 04:42
  • getRealPath() as used in below answer is **bad practice**. See also http://stackoverflow.com/q/12160639 Just use `getResourceAsStream()` on exactly the same path. – BalusC Mar 03 '16 at 14:59

1 Answers1

0

If you have the file in you web root (I assume that you are) then you have to use ServletContext.html#getRealPath(java.lang.String) this will give you the actual path of the file. So your code to create FileInputStream should be.

FileInputStream fstream = 
                new FileInputStream(getServletContext().getRealPath("/database.txt"));
Karthikeyan Vaithilingam
  • 6,473
  • 10
  • 41
  • 60
  • I tried but it is not working still it is going inside catch to show error and i stored both servlet program and database.txt inside webapps\myprograms\ I cant figure out whats wrong where to store the file please help! – Soumyasree Biswas Mar 23 '15 at 05:19
  • @SoumyasreeBiswas please post your folder structure – Karthikeyan Vaithilingam Mar 23 '15 at 05:22
  • Exception report description The server encountered an internal error that prevented it from fulfilling this request. exception java.lang.NullPointerException java.io.FileInputStream.(FileInputStream.java:134) java.io.FileInputStream.(FileInputStream.java:97) servlet1.doGet(servlet1.java:54) servlet1.doPost(servlet1.java:604) javax.servlet.http.HttpServlet.service(HttpServlet.java:644) javax.servlet.http.HttpServlet.service(HttpServlet.java:725) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ."This is the error im getting plz help!" – Soumyasree Biswas Mar 23 '15 at 05:27
  • C>Users>Sony>Tomcat>apache tomcat 8.0.20>webapps>myprograms> inside it my servlet program and database.txt – Soumyasree Biswas Mar 23 '15 at 05:33
  • Can you upload the war some where and give the link? – Karthikeyan Vaithilingam Mar 23 '15 at 05:38
  • Sorry i cant get it what you are asking for? please tell again what should i do? – Soumyasree Biswas Mar 23 '15 at 05:41
  • How are you deploying in the tomcat are you deploying as a `.war` file or as a folder? if war upload the war else if a folder zip it and upload in some file sharing site like dropbox or google drive. dont forget to give permission – Karthikeyan Vaithilingam Mar 23 '15 at 05:44
  • https://drive.google.com/file/d/0ByalLk2w9mMLc3JyZ3F2UDNIdzQ/view?usp=sharing "This is the link i shared it in google drive" My CATALINA_HOME is C:\Users\SONY\Tomcat software\apache-tomcat-8.0.20 and CLASSPATH is C:\Users\SONY\Tomcat software\apache-tomcat-8.0.20\lib\servlet-api.jar – Soumyasree Biswas Mar 23 '15 at 06:12
  • @SoumyasreeBiswas Sorry my bad I forget to add `/` in the start for the file name I have updated my answer. also created a gist with updated servlet1.java https://gist.github.com/seenukarthi/e8a0eb6cafac9912066e – Karthikeyan Vaithilingam Mar 23 '15 at 06:50
  • Thank you so much now the problem for path is solved but yet my output is not coming i m trying to solve it now..still atleast the path problem is solved thanks a lot for that ! – Soumyasree Biswas Mar 23 '15 at 07:06