I'm new to JSP and Servlet so please bear with me for a while!
I have a problem regarding the communication between JSP and Servlet.
I am trying to create a LogIn Page.
This is my Index JSP page for LogIN
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<html>
<head><title> index.jsp</title></head>
<body bgcolor="orange">
<h2> <font color=white> JSP index.jsp at</font>
<%= request.getContextPath () %>
<font color=white>
</font></h2><hr>
<form action="LibrarySysServlet" method ="POST">
<font color=navy><h3>Login</h3>
<table>
<tr>
<td> MemberId </td>
<td align="left"><input type="text" name="memId" length="30"/></td>
</tr>
<tr>
<td> Password </td>
<td align="left"><input type="text" name="password" length="30"/></td>
</tr>
</table>
<br>
<p><input type="submit" value="Submit"/></p>
</font>
</form>
<br>
</body>
</html>
I am really confused on what to put for the form action for the index page when logging in as the destination changes according to whether the logIn is correct or not.
For my Servlet side, i tried to scan in index page and pass it through a function to check whether logIn is correct and redirect it according to the different answer.
However when run, it does not redirect but it just proceeds to whatever was specified in form action.
Short snippet of the code (didn't paste the whole thing because it was too long):
RequestDispatcher dispatcher;
ServletContext servletContext = getServletContext(); //links to index.JSP
String page = request.getPathInfo();
page = page.substring(1);
if ("index".equals(page)) {
if (logIn(request)) {
dispatcher= servletContext.getRequestDispatcher("/result.jsp");
} else {
dispatcher= servletContext.getRequestDispatcher("/Menu.jsp");
}
}
HELP really confused ! WHat am i doing wrong??
RequestDispatcher dispatcher;
ServletContext servletContext = getServletContext(); //links to index.JSP
//check here your username and password and put into a variable true or false
if (variable== true) {
dispatcher= servletContext.getRequestDispatcher("/result.jsp");
} else {
dispatcher= servletContext.getRequestDispatcher("/Menu.jsp");
}
}
it is better off to use reponse.sendRedirect("{url}") not dispatcher.
Also test your logIn method see if it works as you wish
You could leverage the container and use FORM-based authentication. In the web.xml:
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>login.html</form-login-page>
<form-error-page>login_error.html</form-error-page>
</form-login-config>
</login-config>
then in the login.html:
Please log in:
<form method="POST" action="j_security_check">
<input type="text" name="j_username">
<input type="password" name="j_password">
<input type="submit" value="Login">
</form>
You must use j_security_check, j_username, and j_password for this to work.
Related
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 Login Here</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.
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>
<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.
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.
try by changing the script only
<script type="text/javascript">
function openPage(pageURL)
{
window.location.href = pageURL;
}
</script>
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".
I dont get exactly what you are trying to do,but redirect is working with:
response.sendRedirect(request.getContextPath());
or
response.sendRedirect(String url);
This is a Spring MVC project.
So the following JSP
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<c:forEach items="${nameList}" var="studentList">
<tr>
<td><c:out value="${studentList.name}"/> ${studentList.regNo}</td>
</tr>
</c:forEach>
</table>
returns values like
Ramu
Somu
Mamu
Komu
I want to make each value as a post url, so that, if the user clicks any one link, I would like to submit as like the following jsp code does
<form method="post" action="number" id="number">
<div style="text-align: center;" >
<input width="20" type="text" data-validation="numbers" id="regNo" name="regNo" size="30" maxLength="50" placeholder="Enter Register Number">
</div>
</form>
I don't wanna do GET.
How can I do this? Please help me.
Use this
<td><a href='www.xyz.com:port/number?regNo=${studentList.regNo}><c:out value="${studentList.name}"/> </a></td>
And regNo you can get as the request parameter in controller
or
Path parameter in your controller like
<td><a href='www.xyz.com:port/number/${studentList.regNo}><c:out value="${studentList.name}"/> </a></td>
And modify your controller's configuration accordingly.
I have a JSP web application which uses j_security_check. Is it possible to login a specific user to j_security_check programmatically via a JSP page if I know the userid and password? I tried to pass the variables as URL parameters this way...
response.sendRedirect(www.mydomain.com/j_security_check?j_username=someUserName&j_password=somePassword )
...but it doesn't work. Is there any way to do it?
Edit:
here is my login page which works fine right now. I trimmed some of the code for security reasons.
<form name="signon" method="post" action="/j_security_check">
<table width="100%" cellpadding="4" cellspacing="0">
<tr>
<td valign="top" colspan="4">
<h2><%= UI.tr(null, "login_details") %>
</h2>
</td>
</tr>
<tr>
<td valign="top" width="150px">
<%= UI.tr(null, "login_id") %>
</td>
<td valign="top" width="150px">
<%= UI.tr(null, "login_pass") %>
</td>
<td valign="top" width="150px">
<%= UI.tr(null, "login_token_or_captcha") %>
</td>
<td width="100%"> </td>
</tr>
<tr>
<%
if (logins == null) {
%>
<td>
<input type="hidden" name="j_uri" value="/index.jsp">
<input type="text" id="username" name="j_username" size="16" style="width: 150px;" autocomplete="off" <%= username == null ? "" : "disabled value='" + username + "'" %> onblur="return checkCaptcha();">
</td>
<%
} else {
%>
<td>
<select name="j_username" style="width: 150px;">
<%
for (Login login : logins) {
%>
<option><%= login.getUsername() %>
</option>
<%
}
%>
</select>
</td>
<%
}
%>
<td><input type="password" name="j_password" size="16" style="width: 150px;"> </td>
<td><input type="text" id="mypw" name="mypw" size="16" autocomplete="off" style="width: 150px;"></td>
<td><input class="submit" type="submit" name="submit" value="<%= UI.tr(null, "login_submit") %>"></td>
</tr>
<tr>
<td valign="top" colspan="4">
<%-- <%
if("registry.nic.hn".equals(request.getServerName())) {
%>
<!-- GARTH - put whatever you want here for .HN -->
<% } else { %> --%>
<h2><%= UI.tr(null, "login_news") %>
</h2>
<div><%= HTMLFormat.addBreaks(SiteConf.getSiteConf().getNews()) %>
</div>
<%-- <% } %> --%>
</td>
</tr>
The following code works for me in the JSP file:
String url = "j_security_check?j_username=" + username + "&j_password=" + password;
String redirectUrl = response.encodeRedirectURL(url);
response.sendRedirect(redirectUrl);
In my views, login using j_security_check url by appending username and password seems to be a big security venerability.
Instead you can perform following steps:
create a separate JSP which has a form
this form POST on j_security_check url with username and password
you can dynamically include this JSP
this JSP can have onload (JS) function which can POST this form.
In this way it will be secure.
You stated you want to login programatically. I would avoid using j_security_check if this is your goal. j_security_check is used with Form-Based Authentication (Check out this Oracle resource on Securing Web Applications - Form Authentication is covered about half way down: http://docs.oracle.com/cd/E24329_01/web.1211/e24485/thin_client.htm).
I would suggest you take a look at that resource and dive into some of the alternatives, but another approach altogether is to use the Java Authentication and Authorization Service (JAAS) API (Here is a tutorial link: http://docs.oracle.com/javase/6/docs/technotes/guides/security/jaas/tutorials/GeneralAcnOnly.html) JAAS and a callback handler is the non-abstraction way of programmaticly addressing authentication (and authorization). Many robust frameworks are based on this JAVA API, so it might be worth taking a look if you are still struggling with this issue.
You can have the user enter the username and password on a jsp page and a servlet or use a facelets page and a bean as described here to login a user.
I am developing a web application using JSP and Servlets.
I wants to call servlet's method from JSP page when user click's on the Update button.
<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>
When I click on Update button then this method is calling the servlet, But the problem is that when form is loaded then this method is called automatically.
Thanks in advance.....
Source Code....
<%#page import="MyPackageName.MyServletName"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update</title>
</head>
<body>
<form>
<%
String[][] data = (String[][])request.getAttribute("data");
String[] columnNames = (String[])request.getAttribute("columnNames");
//fill the table data
if(columnNames.length!=0 && data.length!=0)
{
%><table><%
}
for(int i=0;i<data.length;i++){
%>
<tr> <td><%= columnNames[0] %></td> <td><input type="text" name="text1" value="<%= data[i][0] %>"></td> </tr>
<tr> <td><%= columnNames[1] %></td> <td><input type="text" name="text2" value="<%= data[i][1] %>"></td> </tr>
<tr> <td><%= columnNames[2] %></td> <td><input type="text" name="text3" value="<%= data[i][2] %>"></td> </tr>
<%
}
%>
<tr>
<td></td>
<td><input type="button" value="Update" onclick="<%PlanProtocolEdit.Update(request, response);%>"></input></td>
</tr>
</table>
</form>
</body>
</html>
Is there any way that I can call the servlet method other than dogGet() and doPost() without invoking the servlets dogGet() and doPost() methods?
You should specify the action on the form itself not the actual input button.
If you define an action on the form. E.g.
The forms submit button will submit the form to that URL. E.g.
<form action="/mypath/someurl">
<input type="submit" value="Submit" />
</form>
You can add an additional attribute to the form tag to specify whether you want the request to be a get or a post.
<form action="/mypath/someurl" method="POST">
Would send a post request, you can then pick this up in your Servlet with the handlePost method
The above approach should be used, currently you are trying to call a Java method on a javascript onclick event. This is incorrect and it is not doing what you think it is.
The code in PlanProtocolEdit. Update should be in a doGet or doPost method of a servlet which will be triggered by configuring your form as described above.
<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>
This line in your jsp will be evaluated to
<input type="button" value="Update" onclick=""></input> in HTML page. If you want to call your servlet on click, first map your servlet to a url path say /myservletpath in web.xml and then
use
<input type="button" value="Update" onclick="location.href='/myservletpath'"></input>
You can call a servlet method other than doPost() and doGet() by creating a useBean of jsp.
<jsp:useBean id="someid" class="SomePackageName.PlanProtocolEdit">
and call the servlet method onclick as:
<input type="button" value="Update" onclick="<% someid.Update(args[0], args[1]); %>" />
This jsp bean would identify your servlet class which you now can access in your jsp page with the id given to it in useBean tag.
Do not forget to close the useBean tag.
Hey guys I'm working on a Apache Tomcat v7.0 servlet where the user enters in some values, gets sent to a page to preview then hits submit. The issue is, once they hit submit I get a 404 saying file non existing.
Set up of project -
WebContent folder has the welcome.js file which sends to roster.js file to preview - within the roster.js file you hit submit and that sends you to this link - src/bandServ/BandListServ.java this link is contained within the Java Resources folder, along with the WebContent folder. The Java Resources folder contains my packages and that's where I want the data to be sent from the form on the roster.js file.
Code of roster.js file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%# include file="/includes/header.html"
%>
</head>
<body>
<h1>Battle Of Bands</h1>
<p>Here is our band roster:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">Band1:</td>
<td><%= request.getParameter("Band1name") %></td>
</tr>
<tr>
<td align="right">Band2:</td>
<td><%= request.getParameter("Band2name") %></td>
</tr>
<tr>
<td align="right">Band3:</td>
<td><%= request.getParameter("Band3name") %></td>
</tr>
<tr>
<td align="right">Band4:</td>
<td><%= request.getParameter("Band4name") %></td>
</tr>
<tr>
</table>
<p>Ready to Rock</p>
<form action="src/bandServ/BandListServ.java" method="post">
<input type="submit" value="submit">
</form>
</body>
</html>
<%# include file="/includes/footer.jsp" %>
It is not possible to invoke a Java class from a form directly.
A class should be created as a Servlet extending the HttpServlet class. Then the deployment descriptor (/Project/WEB-INF/web.xml) has to be modified to include the servlet class details and a url mapped to it.
<servlet>
<servlet-name>BandListServ</servlet-name>
<servlet-class>bandServ.BandListServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BandListServ</servlet-name>
<url-pattern>/list-bands</url-pattern>
</servlet-mapping>
Modify the from in the JSP to call the URL pattern defined in the web.xml.
<form action="/list-bands" method="post">
<input type="submit" value="submit">
</form>
You should provide a servlet mapping that translates URL requests to method calls.
http://docs.oracle.com/cd/E11035_01/wls100/webapp/configureservlet.html
A simpler approach is to use Java Server Pages, which translates the code and does the servlet mappings automatically for you.
[edit]
Adding a link to a tutorial on servlets:
http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html
this line
<form action="src/bandServ/BandListServ.java" method="post">
is worng i belive.
action= " " should contain a uri, which is mapped to a logical servlet name in your web.xml which is intern mapped to fullt qualified class name of the servlet. Instead you cannot specify your servlet name in the action.
your web.xml should contain saomething like this
<servlet>
<servlet-name>BandServlet</servlet-name>
<servlet-class>com.band.BandServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BandServlet</servlet-name>
<url-pattern>/getBands</url-pattern>
</servlet-mapping>
and in the form tag do this
<form action="getBands" method="post">