1

Am trying to link from a JSP to a servlet . On clicking the button with the name="conf" I need to redirect to a servlet "/Initial" . The problem is when I use type="button" nothing happens, while when I use type="submit" the page gets directed to servlet "/Initial" and does the action there. Am not able to identify the problem.

Here is my code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="reg.serv.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <form method="post">
        <center>
            <table border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Register Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Username</td>
                        <td><input type="text" class="" id="username" name="username1" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="password1" id="password" value="" /></td>
                    </tr>
                    <tr>
                        <td>Confirm Password</td>
                        <td><input type="password" name="confirmpassword1" id="confirmpassword" value="" /></td>
                    </tr>
                    <tr>
                        <td>Mobile Number</td>
                        <td><input type="text" class="" id="mob" name="mob1" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email ID</td>
                        <td><input type="text" class="" id="email" name="email1" value=" " /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><textarea id="address" name="address1"></textarea></td>
                    </tr>

                    <tr>
                        <td colspan="2">Already registered <a href="Index.jsp">Login Here</a></td>
                    </tr>
                </tbody>

                <tr>
                    <td><input type="button" value="confirm" name="conf" /></td>
                    <td><input type="reset" value="Reset" /></td>
                    <td><input type="button" value="Cancel" name="Cr" onclick="openPage('Initial.jsp')" /></td>
                </tr>
        </table>
    </form>

    <script type="text/javascript">

        function openPage(pageURL) {
            window.location = pageURL;
        }

    </script>

    <%
        String x = request.getParameter("conf");

        if (x != null && x.equals("confirm")) {
            //response.sendRedirect("/Initial");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/Initial");
            dispatcher.forward(request, response);
        }
    %>

</body>
</html>


Please help me . Any help would be greatly appreciated. Thanking You.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Reenu Rahman
  • 33
  • 2
  • 3
  • 10
  • 2
    Read this: [Difference between and ](http://stackoverflow.com/a/290221/814702) and this
    : [The HTML `
    – informatik01 Jan 08 '15 at 17:53
  • yeah.. @informatik thank you... I tried to redirect using an event onclick of button... but only jsp pages can be retrieved not servlet – Reenu Rahman Jan 08 '15 at 18:07
  • 1
    Seems like you're trying to do it kind of PHP way: mixing presentation and code. Your servlet code should NOT be contained in the JSP file. Also using scriptlets in JSP files is [highly discouraged](http://stackoverflow.com/a/3180202/814702). What you are trying to achieve seems to be very simple scenario and I suspect you're just not very experienced in the Servlets/JSP world. Read this Stack Overflow info page about [Servlets](http://stackoverflow.com/tags/servlets/info) and see there simple servlets usage examples. – informatik01 Jan 08 '15 at 19:28
  • Also here is one of the most popular tutorials: [Beginning & Intermediate Servlet & JSP Tutorials](http://courses.coreservlets.com/Course-Materials/csajsp2.html). Hope those will help you, because ANYWAYS you **must learn such simple things yourself**. Don't be afraid - it's not that scary )) – informatik01 Jan 08 '15 at 19:30
  • 1
    thank you @informatik for the tutorials am new to jsp,servlets and webconcepts etc doesnt have much experience in any of this.. the tutorial helped in clearing the concepts. thank you again – Reenu Rahman Jan 09 '15 at 15:25
  • @Informatik I use Java EE 6 / Servlet 3.0 . Had a review of this page http://stackoverflow.com/tags/servlets/info and found out that there isn't a need to map in web.xml. But without that I could not direct to my servlet although. What's the problem??? – Reenu Rahman Jan 10 '15 at 14:52
  • Since the [*Java Servlet 3.0 Specification*](http://download.oracle.com/otndocs/jcp/servlet-3.0-mrel-eval-oth-JSpec/) you can use annotations, like [@WebServlet](http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html) and there specify URL patterns (for example `@WebServlet("/Initial")`). By the way, the [Servlets info page](http://stackoverflow.com/tags/servlets/info) first gives examples using annotations. – informatik01 Jan 10 '15 at 16:14
  • Also see [this answer](http://stackoverflow.com/a/14225540/814702) that shows different ways of specifying URL patterns (**mapping servlets to URLs**). And here more about Servlet version 3 annotations (by the way this is from a very useful tutorial that have lots of helpful pictures): [Java Server-Side Programming: Servlet 3.0](http://www.ntu.edu.sg/home/ehchua/programming/java/JavaServlets.html#zz-13.) – informatik01 Jan 10 '15 at 16:19

6 Answers6

1

you have to write

<form action=/your_servlet_page_name>

And you have to use

<input type="submit" value="confirm" name="conf"/>

And also you have to map your servlet page into web.xml file like

<servlet-mapping>
    <servlet-name>CheckLogin</servlet-name>
    <url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>
Ram Mansawala
  • 652
  • 7
  • 22
  • Tried it @Ram . Initial /Initial Gets error Caused by: java.lang.IllegalArgumentException: The servlets named [Initial] and [reg.serv.Initial] are both mapped to the url-pattern [/Initial] which is not permitted... – Reenu Rahman Jan 08 '15 at 18:41
  • I use Java EE 6 / Servlet 3.0 . Had a review of this page http://stackoverflow.com/tags/servlets/info and found out that there isn't a need to map in web.xml. But without that I could not direct to my servlet although. – Reenu Rahman Jan 10 '15 at 14:51
  • @ReenuRahman your welcome. vote my answer so it will be helpful to all others. – Ram Mansawala Mar 05 '15 at 08:33
1

<form action = "servlet-name" method = "method in the servlet">
<input type ="submit" value = "val">
</form>

This is a simple way to do this. If you are using the latest jre i think 7 and up, you don't need to declare the servlet in your web.xml file. the @WebServlet("/servlet-url") will do the trick.

Adzz
  • 77
  • 1
  • 6
0

if you want to use type="button" instead of type="submit". you can use javascript function on the click on the button. Like

<script>
function doSubmit(){
var actionURL ="MENTION URL YOU WANT TO REDIRECT";
// perform your operations

myForm.submit(actionURL); OR
myForm.submit();
}

</script>
<form name="myForm">
    <input type="button" name="conf" value="conf" obclick="doSubmit();">
</form> 

hope it will help you.

0

try by changing the script only

<script type="text/javascript">
 function openPage(pageURL)
 {
 window.location.href = pageURL;
 }
</script>
Abhijit Bashetti
  • 8,518
  • 7
  • 35
  • 47
0
function openPage(pageURL) {
            window.location = pageURL;
        }

In above code snippet, pageURL has to be an absolute URL which in case of dealing with servlets may vary.So instead of this below can be used

location.href = (location.href).substr(0, (location.href).lastIndexOf('xyz.jsp'))+"/abc";

Here 'abc' is the servlet to which we have to redirect the 'xyz.jsp' .This can even work if there are many buttons.The respective functions could be written to redirect to respective servlets. Also it works in case of input type is "button" or "submit".

Chetna R
  • 1
  • 1
-1

I dont get exactly what you are trying to do,but redirect is working with: response.sendRedirect(request.getContextPath()); or

response.sendRedirect(String url);
greedsin
  • 1,252
  • 1
  • 24
  • 49
  • need to pass the values from the form to a servlet "/Initial"... need to use a "button" instead of "submit" for transferring the form values. I used onclick but it works for redirecting to a jsp only not to a servlet – Reenu Rahman Jan 08 '15 at 17:58
  • Ok try to write an external Servlet Class und use
    – greedsin Jan 08 '15 at 18:01
  • I tried that already @lolio still nothing happens when button is used while when submit is used error occurs type Status report message /reg1/jsp/Initial description The requested resource is not available. – Reenu Rahman Jan 08 '15 at 18:06
  • did you change from type="button" to type="submit" ? and is your servlet also mapped in web.xml? Ram has already a nice answer for you – greedsin Jan 08 '15 at 18:09
  • I tried it. Gets error....Caused by: java.lang.IllegalArgumentException: The servlets named [Initial] and [reg.serv.Initial] are both mapped to the url-pattern [/Initial] which is not permitted – Reenu Rahman Jan 08 '15 at 18:41