Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Redirect if invalid URL entry

Reply
Thread Tools

Redirect if invalid URL entry

 
 
teser3@hotmail.com
Guest
Posts: n/a
 
      05-18-2007
On my Tomcat 4.1.27 container, the below Pagination works great using
a Servlet where page number is entered in the Browser URL area and has
to be a
digit:
http://127.0.0.1:8080/theWeb/mypackage/page/1
Every manual URL entry after the page/ part has to be a number only.

But sometimes someone might manually input a non digit entry (
http://127.0.0.1:8080/theWeb/mypackage/page/2d ) in the URL and it
will ge me error message:
java.lang.NumberFormatException: For input string: "2d"


Here is the Servlet and I cant seem to create a redirect message if
someone enters a non digit number:


package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;


public class TheServlet extends HttpServlet{


public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
PrintWriter out = res.getWriter();
out.write("<hr>");
String pg = req.getPathInfo();
out.write("page # " + pg + "<br />");
HttpSession sess = req.getSession();
ArrayList items = null;
int j = 0;
int recordTotal = 0;


items = new ArrayList();
sess.setAttribute("items", items);


try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://
localhost/myf?user=deve&password=mypwd");
Statement stmt = connection.createStatement();
ResultSet results = stmt.executeQuery("SELECT * from user");


while(results.next())
{
recordTotal++;
String myd = results.getString("lastname");
items.add(myd);



}
}


catch(Exception e) {
System.err.println(e);


}


int perPage = 3;
int displayPages = (recordTotal + perPage - 1)/perPage;

pg = pg.substring(1);


j = Integer.parseInt(pg) * perPage - perPage;


Object temp = sess.getAttribute("items");
if(temp instanceof ArrayList)
{
items = (ArrayList) temp;



}


else
{
out.write("Type Cast error. Send status code 500.");


}


//items = (ArrayList) sess.getAttribute("items");

for (int i = 0; i < perPage && i + j < recordTotal; i++) {
if (items != null) out.write("<br>" + items.get(i + j) + "\n");



}
}
}


Servlet mapping in web.xml

<servlet-mapping>
<servlet-name>TheServlet</servlet-name>
<url-pattern>/mypackage/page/*</url-pattern>
</servlet-mapping>


Any suggestions or way for me to redirect a non digit url entry?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect if invalid URL entry teser3@hotmail.com Java 3 05-20-2007 09:15 PM
Redirect if invalid URL entry teser3@hotmail.com Java 0 05-18-2007 02:19 AM
Redirect url if invalid entry into URL teser3@hotmail.com Java 0 05-18-2007 02:12 AM
redirect URL's, return URL's, and URL Parameters Jon paugh ASP .Net 1 07-10-2004 05:29 AM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57