Frederik Nielsen

Greenhorn
+ Follow
since Aug 25, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Frederik Nielsen

Thanks mate for all your replies. I would probably have given up if it wasnt for you :)

I figured it out.
I just needed to change the code on the error.jsp page to the following



So now it can finally handle exceptions :)
10 years ago
JSP
I think i am getting close to having solved this.

I have


below the success in the AJAX i have posted above.

It shows
<h1>javax.servlet.ServletException: exception</h1>
in the alert (and the rest of the error.jsp page including a full stacktrace.)

but when it redirects it shows
<h1>org.apache.jasper.JasperException: java.lang.NullPointerException</h1>

So it has lost the original exception cause, and lost most of the stacktrace

Any ideas to how i can preserve it ?

10 years ago
JSP
I have just figured out why nothing was happening.

It was because of the jQuery AJAX.

The problem is that i have a form that has a jQuery post method like this:



When the servlet or filter throws an exception it will be ajax that receives it and then throws it in the toilet.

Any idea of a solution for this? (the solution cant be to remove all the jQuery, because that would just kill everything on the site)
10 years ago
JSP
my Servlet controller and filter has

in their catch block now and in my web.xml do i have


It does absolutely nothing.

here is the exception i get in the console in netbeans
10 years ago
JSP
Right now i only have this in web.xml



What do you do about exceptions that has to be caught like sql exceptions ?

Are you able to go from anywhere in your project when an exception happens and then get to an error page where it displays the error ? give a little more info/code about how.
10 years ago
JSP
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

10 years ago
I'm having some problems figuring out how to handle Exceptions when working with JSP / FilterServlet / ControllerServlet

I want all exceptions, no matter if they happen in JSP page/FilterServlet/ControllerServlet/Java Class to redirect the user to "error.jsp" where it shows the error name and a stacktrace of it.

This is a typical action on the site:
1. user requests "index.jsp"
2. FilterServlet checks session and gets data to display on "index.jsp"
3. when FilterServlet is done it shows "index.jsp"

1. user clicks update button
2. jquery ajax makes a post to ControllerServlet
3. ControllerServlet ask a Java Class to open a connection to mysql and make the update
4. ControllerServlet gets a boolean back from the class wheather it was a success or not
5. ControllerServlet makes a printwriter where it prints the result
6. jquery ajax reads the result and displays it to the user in a nice message under the update form.

All of the above works like it should except for when an exception occours.
right now when an error occours i just make a try catch and do nothing in the catch block.
i have tried almost all of the tutorials and examples i could find on how to handle this but none of them seams to work.

The problem might be that everything happens through jQuery AJAX, but i am not sure.

in a sidenote i am also using URLRewriteFilter, but i dont think that gives any problems when it comes to exceptions

I would be very grateful if someone here can figure this out.

Regards Frederik
10 years ago
JSP
Yes it is possible, and you can do it in many ways.

What is your requirements?
what is gon? it is something static? if so make it a hidden field. is it not static? what is it then? how does it change? what makes it change?

should gon be send when you hit a submit button in a form ?

It is hard to say what the best solution is for you without the excact requirements.
10 years ago
JSP

Is the above code in the same JSP file? and you should write it in this order <html><head></head><body></body></html>

also your javascript is after </head> and before <body> . it should be between <head> and </head>

If it still dont work after that edit your post and i will take another look at it.
10 years ago
JSP
Here is a working solution:

Login.jsp


User.jsp



Have tested it and it works, but you should look into Servlet for request/response :)
10 years ago
JSP
It sounds like you are looking for a Servlet Filter class.

It looks like this:



Then in your web.xml you have this code:



Now every single request on your site will first have to go through the filter. What what? can it be that simple? YES :)
Now in your filter you just have to read through the XML file and then pass it in a session or just the request to the JSP that can then display it

This also ensures that the user wont be able to get to the JSP if there is an error in your XML file which could lead to a complete crash of the page.

I use a filter to get all the data for all my JSP pages and also creating sessions and objects needed for the JSP pages.
Then the JSP pages ONLY job is to display data :)
10 years ago
JSP
I completely agree with K. Tsang, but instead of just throwing a ton of names at you with things you should learn, do i suggest that you check out http://thenewboston.org/list.php?cat=31 . It is a guy called Bucky that has made a ton of videos for people completely new to java. He makes it very fun to learn since he is a very funny guy and makes some great tutorials.

Just follow his tutorial videos from the beginning and then when you are done you can check out his other java tutorials for more skilled programmers.
I have used his tutorials myself and have learned a lot from them.

If you get stuck on some problem you cant figure out, i suggest you try to google it. you will be amazed how many people has had the excact same problem :)
10 years ago
JSP
This is what happens when you 100% mix server code with client code.

jsp pages are ONLY for displaying data. you are not supposed to make database request directly in them. Just because you can, does not mean you should.

- What you should do is to make a Servlet Filter that gets called before the jsp is loaded.
- In Servlet Filter - make a call to a Servlet Controller (or do it directly in the filter if you are lazy) to get the data you want to display on the jsp.
- In Servlet Controller - make a call to the Data Access Object classes needed
- In Data Access Object class - get a connection from Database class, then get the data, close connection and return it
- when the data has been returned back to the Servlet Filter, can you make a session or just pass the data in the request to the jsp.
- In JSP - read data from session or request and display it.

I know this might seam like overkill, but it is just good practice to seperate the different parts to the place they belong.
You should take a look at https://datatables.net/

I am using that on my table in JSP.
There is tons of nice features like:
- sorting (asc/desc with icons for them and you can disable it for columns you dont want it on)
- dropdown with number of threads displayed on each page
- label that displays message like "Showing 1 to 10 of 69 entries" and when you go to next page it will show "Showing 11 to 20 of 69 entries"
- search (you can choose which fields sould be searchable and it can search in multiple)

You can also customise the look of it.
Takes a little time to learn to use, but when you have it under control you will never go back to a non jQuery powered table

The sky is the limit.
I have also added images and update and delete buttons in mine that works perfectly fine.

jQuery for the win!
10 years ago
JSP