<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Enter Your Data!</TITLE>
</HEAD>
<script type="text/javascript">
function changeVal()
{
document.getElementById("textfield2").value="abc";
}
</script>
<BODY>
<form method="GET" action='index.jsp' name="good">
<input type="submit" name="submit" value="submit" id="textfield2" />
<input type="text" name="done" value='<%=request.getAttribute("k")%>' />
<% String k=request.getParameter("done");
request.setAttribute("k",k);
%>
</form>
</BODY>
</HTML>
This is my simple index.jsp file code am unable to display text after Button click please Help me how i ll do this after Button click Text disappointing of iN put value
If I understood correct, you want to display submitted value.
Try to change
<input type="text" name="done" value='<%=request.getAttribute("k")%>' />
to
<input type="text" name="done" value='<%=request.getParameter("done")%>' />
But it's better not to do any logic in jsp. Try to move all logic into servlet or any other controller.
You may need to call the changeVal() somewhere in your code, probably in onload of the body.
<script type="text/javascript">
<%
String paramValue = request.getParameter("submit");
paramValue = (paramValue==null)?"":paramValue;
%>
var buttonValue = <%=paramValue%>;
function changeVal()
{
if(buttonValue!="")
{
document.getElementById("textfield2").value = buttonValue;
}
}
function formSubmit()
{
//set the value need to be changed
document.getElementById("textfield2").value="abc";
document.getElementById("good").submit();
}
</script>
<BODY onload="changeVal()">
<form method="GET" action='index.jsp' id="good" name="good">
<input type="button" onclick="formSubmit()" name="submit" value="submit" id="textfield2" />
<input type="text" name="done" value='<%=request.getAttribute("k")%>' />
<%
String k=request.getParameter("done");
request.setAttribute("k",k);
%>
</form>
</BODY>
Related
I can't get the checkbox value of "no_del_file" into the servlet.
here is my JSP:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>List of Command menu</title>
</head>
<body>
<div><b>${reply}</b></div>
<div id="wrapper">
<div id="menu">
<form action="runButtonCommand" method="post" name="myform">
<a href="runButtonCommand?param1=list apps" >List1</a><br/><br/>
<a href="runButtonCommand?param1=list data" >List2</a><br/><br/>
<br/><br/>
no del:
<%String test = (String)request.getParameter("no_del_file"); %>
<%String checked = "";%>
<%
if ("on".equals(test)) {
checked="checked=\"on\"";
} %>
<input type="checkbox" name="no_del_file" <%=checked%>>
<br />
</form>
</div>
<div id="output">
<p>Output:</p>
<textarea style="resize: none;" data-role="none" rows="40" cols="120" name="outputarea">
${data}
</textarea>
</div>
</div>
</body>
</html>
And here is the Servlet part where I try to get the checkbox value:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String[] tester= request.getParameterValues("no_del_file");
System.out.println("Start: "+tester);
performTask(request, response);
}
However it doesn't matter if I use getParameterValues, getParameter or getAttribute I always get null if checked or unchecked. How can I get that value?
Thanks for your help.
Viking
This is my code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Select Payment Method<BR><BR>
<font size=""><marquee behavior="alternate">PAYMENT GATEWAY</marquee></font>
<form action="S2ShowPay.jsp" method="post">
<input type="radio" value="NET BANKING" name="payment" >NET BANKING<br><br>
<input type="radio" value="DEBIT CARD PAYMENT" name="payment">DEBIT CARD<BR><br>
<input type="radio" value="CASH ON DELIVERY" name="payment" >CASH ON DELIVERY<br><br>
<input type="submit" value="NEXT"/>
</form>
</body>
</html>
I want to navigate to netbanking.jsp when I click on Net Banking radio button and Debit.jsp repectively, using only jsp and some javascript.
Please help.
Im not sure what's your expected result, Perhaps you can try this. Correct me if im wrong.
Using location.href
function fnPayment()
{
if(document.getElementById("a").checked ==true || document.getElementById("b").checked ==true)
{
location.href = "http://www.google.com"; //your jsp file
}
}
</script>
<input type="radio" value="NET BANKING" name="payment" id="a">NET BANKING<br><br>
<input type="radio" value="DEBIT CARD PAYMENT" name="payment" id="b">DEBIT CARD<BR><br>
<input type="radio" value="CASH ON DELIVERY" name="payment" id="c">CASH ON DELIVERY<br><br>
Using response redirect
Since you have created S2ShowPay.jsp, you can pass parameter to it once you clicked next button and put response redirect based on the parameter.
<input type="radio" value="NET" name="payment" >NET BANKING<br><br>
<input type="radio" value="DEBIT" name="payment">DEBIT CARD<BR><br>
<input type="radio" value="CASH" name="payment" >CASH ON DELIVERY<br><br>
S2ShowPay.jsp
String payment_ind = request.getParameter("payment");
if(payment_ind.equals("NET") || payment_ind.equals("DEBIT") )
{
response.sendRedirect("netbanking.jsp");
}
Add this code within head tag:
<script type="text/javascript">
function redirectPage(use,rname){
for (var val = 0, r1=use.elements; val < r1.length; val++)
if(r1[val].name==rname&&r1[val].checked)
use.action=r1[val].value;
}
</script>
Change your form tag like this:
<div>
<form action="#" method="post" onsubmit="redirectPage(this, 'r1');">
<input type="radio" name="r1" value="netbanking.jsp">NET BANKING<br><br>
<input type="radio" name="r1" value="Debit.jsp">DEBIT CARD<BR><br>
<input type="radio" name="r1" value="cashondelivery.jsp">CASH ON DELIVERY<br><br>
<input type="submit" value="Next"/>
</form>
</div>
Select the desired radio button and then clicking Next button will redirect to respective page.
Last few hours i am trying to solve this but i can not.I am sending ajax request using jquery and based on response i set data on jsp.actully i am checking login detail so if login fail it set error message on label problem is that error message is set for few second and removed i mean to say error message is set for few second i want that if login fails the message is set on label still user enter valid details
thanks in advance
Here is my code
LoginDemo.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<link href="css/firstpage.css" rel="stylesheet" type="text/css" /><!-- <style> -->
<script src="js/jquery.min.js"></script>
<title>Insert title here</title>
<script>
$(document).ready(function(){
$("#submit").click(function(){
$.ajax({
type: "POST",
url: "AddInspServlet",
cache:false,
data: { userid: $('#username').val(), password: $('#password').val(),btn:$('#submit').val() },
success:function(data,textstaus){
alert("success:"+data);
if(data == "no"){
alert( document.getElementById("error").innerHTML);
document.getElementById("error").innerHTML= "UserName OR Password is incorrect";
alert( document.getElementById("error").innerHTML);
}else{
location.href = 'Home.jsp';
}
},
error:function(data){
alert("error"+data);
},
});
});
});
</script>
</head>
<body>
<form id="login" name="Loginform" method="post">
<h1><b>Log In</b></h1>
<fieldset id="inputs">
<input id="username" type="text" placeholder="Username" autofocus required>
<input id="password" type="password" placeholder="Password" required>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="login">
Forgot your password?
</fieldset>
<label id="error" style="color: red;font: bolder; font-size: 18px;">
</label>
</form>
</body>
AddInspServlet
Here i m adding code which server executed and retrieve response
if(btn.equals("login"))
{
System.out.println("***** login condition checking ******");
String password =request.getParameter("password");
UserVO v =op.loginCheck(username, password);
if(password.equals(v.getPassword()))
{
System.out.println("inside true condition");
HttpSession session = request.getSession();
session.setAttribute("userid",v.getUser_id());
session.setAttribute("username",v.getUsername());
session.setAttribute("user", v.getFirst_name()+" "+v.getLast_name());
session.setAttribute("roleid", v.getRole_id());
// response.sendRedirect("Home.jsp");
System.out.println("submitting data success fully");
out.print("yes");
}
else
{
System.out.println("false condition");
out.print("no");
}
}
Get rid of the form tags.
By adding a type="submit" input element without using the action attribute in the form tag, the page will be reloaded.
Or you could keep the form tags and change the type of the submit button to type="button". The form will then not be executed and the page will not reload.
It would be better if you be more specific about the problem. I assume your problem is Label is getting displayed for few seconds and then get disappeared. Is that is true? then try to return "false" inside you data == "no" logic.
Thanks.
code:
<script type="text/javascript">
function showFileName() {
var filename = document.getElementById("uploadFile");
}
</script>
OR
<script type="text/javascript">
var filename = document.getElementById("uploadFile");
</script>
OR
var filename="uploadedfilename";
<form name="AttachmentsForm" method="post" action="<%=Constants.WEB_APP_NAME%><%=Constants.SERVLET_NAME%>?para=ajaxRefTabUpload&action=add&uploadfilename="+filename+"" ENCTYPE="multipart/form-data">
<table class="innerBorderTable" width="100%">
<tr>
<td>Attach New File:</td>
<td>
<INPUT TYPE="FILE" NAME="uploadFile" width="120">
<input type="submit" class="button" value="Add Attachment">
</td>
</tr>
</table>
</form>
I tried 3 different approaches to pass it with +filename+ i am getting null with uploadfilename in action parameter
Please advise
use of scriptlets(<% %>) in JSP highly discouraged, use JSTL(<c:tags) or Expression Language(${})
to use JSTL in your jsp download jstl.x.x.jar file and add it to your buildpath.
then, add this
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
in top of jsp.
to resolve form action url you have:
<%=Constants.WEB_APP_NAME%><%=Constants.SERVLET_NAME%>
no need of any constants for that, instead use <c:url tag to resolve url like:
<c:url value="/yourServletUrl" var="postUrl"/>
<form action="${postUrl}" method="post" ...
i am getting null with uploadfilename in action parameter
because, you accessing javascript variable in html(&uploadfilename="+filename+"), instead do like:
jsp:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>your title</title>
</head>
<body>
<c:url value="/yourServletUrl" var="postUrl"/>
<form id="AttachmentsForm"
method="post"
onSubmit="showFileName()"
action="${postUrl}para=ajaxRefTabUpload&action=add"
enctype="multipart/form-data">
<table class="innerBorderTable" width="100%">
<tr>
<td>Attach New File:</td>
<td>
<input type="file" name="uploadFile" width="120">
<input type="submit" class="button" value="Add Attachment">
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function showFileName(e) {
if (e.preventDefault)
e.preventDefault();
var filename = document.getElementById("uploadFile");
alert('fileName: '+filename);
//here, attach filename to from action and continue your form submition by AJAX
// You must return false to prevent the default form behavior
return false;
}
}
</script>
</body>
</html>
Try something like:
function showFileName() {
document.forms[0].action ="<%=Constants.WEB_APP_NAME%>... //Here goes the expression you want to set the action to
}
My project is StaffAllocation, and I want to retrieve information from the database. I'm very new and this is my very first project. I created a drop down list retrieving staffnames from one of my table. Now I want to perform a query action to view the details of the selected staffnames from the drop-down list. The following is the coding which i have, which is not correct:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.sql.*" %>
<%# page import="java.io.*" %>
<%# page import="java.lang.*" %>
<%# page import="javax.servlet.*" %>
<%# page import="javax.servlet.http.*" %>
<%ResultSet resultset =null; %>
<!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>Staff Details</title>
</head>
<BODY>
<form method=post>
<h3>Select Stafftype:</h3>
<p><input type="radio" name="Stafftype" value="Male"> Male</input></p>
<p><input type="radio" name="Stafftype" value="Female"> Female</input></p>
<input type="submit" value="Submit">
</form>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation? user=root&password=success");
Statement statement = connection.createStatement() ;
String Stafftype= request.getParameter("Stafftype");
out.print(Stafftype);
if(Stafftype.contentEquals("Male")){
resultset=statement.executeQuery("select * from tblstaffdetails where Stafftype= 'Male'");
}
else if(Stafftype.contentEquals("Female")){
resultset=statement.executeQuery("select * from tblstaffdetails where Stafftype= 'Female'");
}
else
{
System.out.println("your coding is wrong");
}
%>
<select> <% while(resultset.next()){ %>
<option><%= resultset.getString(2)%></option>
<%} %>
<%
String StaffName= request.getParameter("StaffName");
int staffId;
String subcode;
if(StaffName != null) {
resultset=statement.executeQuery("SELECT a.staffId, a.StaffName, b.subcode FROM tblstaffdetails a LEFT JOIN tblsubhandled b ON a.staffId = b.staffId where StaffName='request.getParameter('StaffName')'");
}
}
catch(Exception e)
{
out.println("wrong entry"+e);
}
%>
<form method = "get">
<br><br>
<input name="Submit" type="button" value="Submit">
</form>
</body>
</html>`
Tables:
tblstaffdetails -(1).staffId(2).StaffName(3).Stafftype(male or female)
tblsubhandled - (1).staffId(2).subcode
I have fiddled sample implementation for your reference. You can implement like that. All you need to do is include jQuery plugin. Sample code lke this,
$.ajax({
url : 'ur_servlet_url' + selValue,
type : "POST",
async : false,
success : function(data) {
//Sample data
var data = "<select id='child'>
<option value='11'>Value11</option></select>"
$("#fillValue").html(data);
}
});
You need to compose your java response like select tag and return it to your ajax response. Finally you can fill the second dropdown like that. Let me know if this helps.
<form method="post" action="select.jsp">
<select name="sell">
<option value="Alto">Alto</option>
<option value="Esteem">Esteem</option>
<option value="Honda City">Honda City</option>
<option value="Chevrolet">Chevrolet</option>
</select>
<br>
<input type="Submit" value="Submit">
</form>
<%
String st=request.getSelectedIndex("sell");
if(st!=null){
out.println("You have selected: "+st);
}
%>