Java Simulate 500 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us how to create 500 internal server error Learn more about Stack Overflow the company Business Learn more about hiring developers
How To Trigger 500 Error
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack internal server error 500 Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Java servlet throw http 500 error on purpose 503 error up vote 1 down vote favorite I am developing a java servlet. I am using glassfish server 4. End users are sending me information through URL parametars, something like this: http://myIP:8080/TestProject/TestServlet?param1=test1¶m2=test2¶m3=test3 I am getting the values from param1, param2 and param3 and i want to write them in my database. If I get a SQL exception while writing the informations in my db i want to throw "500 Internal Server Error" to let them know that i have some technical problems and to resend their request. I want to know is there a default way to do this, set some status, display text ...? Here is the code: @WebServlet(urlPatterns = {"/TestServlet"}, initParams = { @WebInitParam(name = "param1", value = ""), @WebInitParam(name = "param2", value = ""), @WebInitParam(name = "param3", value = "")}) public class TestServlet extends HttpServlet { String param1; String param2; String param3; boolean dbOK; /** * Processes requests for both HTTP * GET
and * POST
methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Get parametars from the request param1 = request.getParameter("param1"); param2 = request.getParameter("param2"); param3 = request.getParameter("param3"); //Input in db dbOK = Database.saveParams(param1,param2,param3); //Print response response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println("
dbOK=" + dbOK + "
"); out.println(""); out.println(""); } finally { out.close(); } } //here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us http://stackoverflow.com/questions/4838652/how-to-simulate-a-site-down-in-java Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to http://www.theserverside.com/discussions/thread.tss?thread_id=16249 simulate a site down in Java? up vote 7 down vote favorite 1 My app accesses info on the web, during my development how do I simulate a site is down in Java ? If the url gets me the page my 500 error app processes it, but if a site for some reason is temporarily down, or even if the site is up, but the url returns invalid info or it could be the Internet connection is disabled, how can a Java app distinguish those situations ? And be able to tell which is occurring ? Edit : I'm trying to write my Java app so that when it encounters different url accesses, it knows which case it is dealing with and tell user accordingly. java http-status-code-404 httpurlconnection share|improve internal server error this question edited Jan 29 '11 at 18:47 asked Jan 29 '11 at 18:31 Frank 10.7k38111185 Could you clarify your question, please? Are you trying to test the behavior of your app when it tries and fails to reach an external site? Are you trying to write code that handles some different kind of failure case? –Matt Ball Jan 29 '11 at 18:36 add a comment| 5 Answers 5 active oldest votes up vote 0 down vote accepted If a site is down it will a 404 code and it will throw a java.net.UnknownHostException. You will need to catch it or catch as IOException. For other status codes you can read them using the following code snippet. You can see a list of HTTP status codes here. You can read the response code this way. URL url = new URL ("http://someurl.doesnotexist.com"); URLConnection connection = url.openConnection(); connection.connect(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int code = httpConnection.getResponseCode(); //do something with the code share|improve this answer edited Jan 29 '11 at 19:02 answered Jan 29 '11 at 18:34 CoolBeans 15k95383 4 If the site is down this means the web server is down and not running. It cannot obviously response with a 404 code if it isn't running at all. –Progman Jan 29 '11 at 18:49 Obviously. In that case you will get an exception. The above code is just a quick snippet ... they will need to be surrounded by try catch of IOException. The 404 will get caught in tha
Java testing Databases Java development in cloud Java DevOps Modular code EJB specification Modular containers Modular troubleshooting Programming code App servers Modular tools Java in the cloud Java Web services WS* specs Architecture Java middleware Spring Hibernate Servlets JSP Java client platforms Framework JSF Web portals UI Enterprise Java development JSRs and APIs Languages JVM languages DSLs Java app deployment Java tools Java performance Java performance Cloud platforms JVM tuning Java HPC Web dev tools Development tools Open source strategy IDEs Java platforms Java testing Development hardware Mobile app dev Mobile development and deployment Mobile cloud apps Mobile platforms Mobile ALM Mobile app testing SEARCH this site and the web Site Index Home>Discussions>Web tier: servlets, JSP, Web frameworks>How Tomcat handles 500 Error ? Email This Discussions Web tier: servlets, JSP, Web frameworks: How Tomcat handles 500 Error ? Get thread feed How Tomcat handles 500 Error ? (2 messages) Posted by: david h Posted on: October 29 2002 22:38 EST Hi, I have apache1.3.27 and Tomcat 3.3-m4 installed on my Win2000 server. I have an application deployed and everything works fine except one thing: If I try a URL which doesn't exist, it return me error code 500 and an internal servlet error instead of my default jsp page. My web.xml looks like: ...