How to Loop form elements in JSP - html

I'm want to use some sort of loop to go over all input fields in my form . But I'm unable to find any proper documentation on how to do it. Is it possible to loop over all the elements in my form in some way as well as dealing with multiple checkboxes ?
I tried to follow some online sources . This one outputs null value.
<html>
<body>
<form action = "main.jsp" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" checked = "checked" /> Maths
<input type = "checkbox" name = "physics" /> Physics
<input type = "checkbox" name = "chemistry" checked = "checked" /> Chemistry
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>
<%# page import = "java.io.*,java.util.*" %>
<html>
<head>
<title>HTTP Header Request Example</title>
</head>
<body>
<center>
<h2>HTTP Header Request Example</h2>
<table width = "100%" border = "1" align = "center">
<tr bgcolor = "#949494">
<th>Param Name</th>
<th>Param Value(s)</th>
</tr>
<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getHeader(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
%>
</table>
</center>
</body>
</html>
Is there any documentation source from where I can read about request object's methods ?

Related

How can I perform different submit in HTML form with Delphi?

I'm writing a software to automatically fill fields in a TWebBrowser and make the related Submit.
Unfortunately, the form contains three buttons that perform different types of submit.
Clearly, this doesn't work:
form: = WebFormGet (formNumber, WebBrowser1.Document AS IHTMLDocument2);
form.submit;
I spotted this line of code at the top of the HTML page:
<form name = "RicercaIMMForm" method = "post" action =
"https://sister.agenziaentrate.gov.it/Visure/vimm/RicercaIMM.do" onsubmit = "this.scelta.disabled = true;">
and this at the bottom:
<td> <input type = "submit" name = "choice" value = "Search"> </td>
<td> <input type = "submit" name = "choice" value = "Visura"> </td>
<td> <input type = "submit" name = "choice" value = "Clean"> </td>
<td> <input type = "submit" name = "choice" value = "last search"> </td>
I should have submit the button with "Search" value.
...
...
...
Can you help me?
UPDATE 17/03/2020
I solved with this:
procedure TFMain.WebFormSubmit(const document: IHTMLDocument2;
const formNumber: integer);
var
Doc: IHTMLDocument2;
WebForm: IHTMLFormElement;
FormElements: OleVariant;
i: integer;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
WebForm := Doc.Forms.Item(0,'') as IHTMLFormElement;
FormElements := WebForm.Elements;
FormElements.Item(19).Click;
end;
Item(19) is the number of my object (in this case, the button)

how to get values from checkbox and pass it to the another jsp page?

I have created many checkboxes using a for loop. Now I want to get the values from checkboxes if they are ticked or not.
When a checkbox is ticked, then its label must be passed to another JSP page. But I am unable to achieve it properly.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1><center>REGISTER FORM</center></h1>
<%
String[] stArray=new String[40];
ArrayList ar = new ArrayList();
int idcounter = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/registerdb", "root", "");
PreparedStatement ps = con.prepareStatement("select leave_types from leaves");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String array_value = rs.getString("leave_types");
ar.add(array_value);
}
// out.println(ar);
request.setAttribute("LEV_ARRAY", ar);
}
catch(Exception e)
{
out.println(e);
}
%>
<form action = "insertdata.jsp" name="myform" method="post">
<%
for(int i = 0; i<ar.size(); i++)
{
out.println(ar.get(i));
%>
<input id ="<%=idcounter%>" type="checkbox" name = "" value="" />
<%
idcounter++;
}
// String[] selectedCheckboxes = request.getParameterValues("selected");
%>
<center><button type= "submit" name="action">SIGN UP</button></center>
</form>
</body>
</html>
You can do like below :
Pass ar.get(i) as values in checkboxes :
<form action = "insertdata.jsp" name="myform" method="post">
<%
for(int i = 0; i<ar.size(); i++)
{
out.println(ar.get(i));
%>
<!--name=abc will be used in jsp to get value selected in checkboxes-->
<input id ="<%=idcounter%>" type="checkbox" name = "abc" value="<%=ar.get(i)%>" />
<%
idcounter++;
}
%>
<center><input type= "submit" name="action" value="SIGN UP"/></center>
</form>
Then ,to get above values in jsp page do like below:
<!--getting values selected using "abc"-->
<%String check[]= request.getParameterValues("abc");
<!--checking for null values-->
if(check!= null){%>
<!--there might be more one checkbox selected so, using loop-->
<%for(int i=0; i<check.length; i++){%>
<!--printing values selected-->
<%=check[i]%>
<%}
}%>

Html form and Rest Api Django

I am sending a Username and password from my HTML form to django backend .
<html>
<style type="text/css">
#center_align{
width: 200px;
margin: 10px auto;
}
</style>
<head>
<title>Login Form</title>
</head>
<body>
<div id = "center_align">
<h1>Login</h1>
<form method = "GET" action="http://127.0.0.1:8000/login/" >
Username :<input type ="text" name = "username"><br>
Password :<input type ="password" name = "password"><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
In my django , i have wrote a class in my Views
class Login(APIView):
global User_Grps
def get(request,self):
state = ""
username = "Gauss"
password = settings.AUTH_LDAP_BIND_PASSWORD
oLdap = LDAPBackend()
try:
User = oLdap.authenticate(username=username,password=password)
print User.ldap_user.group_dns
if User is not None:
User_Grps = User.ldap_user.group_dns
else:
User_Grps = "Invalid Group"
except Exception as e:
User_Grps = "Error"
return HttpResponse(User_Grps)
How would i retrieve my username and password from the request object ? Like i need to get the data from the request parameter of the method .
Instead of doing that, you should write a custom Django authentication backend which would let you use the default login views from DRF and/or other Django 3rd parties.

Program will not read my if statement

I am doing this project and I am almost done. It took me a while to actually get the code to actually run but I finally got it. My assignment was to make a HTML form, connect it to MySQL using JSP, and insert the values into the database or delete the values from the database. It is also password protected. Like I said I have gotten it to compile without error however the If statement I have implemented is not being picked up. I was wondering if you all could help me figure why this is happening. Im going to paste my HTML code then my JSP.
HTML CODE
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title> Assignment 3 </title>
</head>
<body>
<h3> Song Name Form </h3>
<form action = "hgooding.jsp" method = "post">
<table border = "1">
<tr>
<td>Song Title: </td>
<td> <input type = "text" name = "song"/> </td>
</tr>
<tr>
<td>Artist </td>
<td> <input type = "text" name = "artist"/> </td>
</tr>
<tr>
<td>Password: </td>
<td> <input type = "password" name = "pass1"
size = "20"/> </td>
</tr>
</table>
<br>
<input type = "radio" name = "option" value = "add"/> Add
<input type = "radio" name = "option" value = "delete"/> Delete
<br>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
JSP CODE
<%# page import = "java.sql.*" %>
<html>
<head> <title> Database jsp </title></head>
<body>
<%
String connectionURL = "jdbc:mysql://sql.njit.edu:3306/hg33";
Connection connection = null;
Statement stm = null;
ResultSet rst=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "hg33", "grapes34");
String song = request.getParameter("song");
String artist = request.getParameter("artist");
String action = request.getParameter("option");
String pass2 = request.getParameter("pass1");
Statement stminsert = null;
stminsert = connection.createStatement();
if (pass2 == "apples4")
{
if (request.getParameter("action").equals("add"))
{
String sqlupdate1=("INSERT INTO Songs VALUES('"+song+"', '"+artist+"')");
stm.executeUpdate(sqlupdate1);
out.println("Hi!");
}
if (request.getParameter("action").equals("delete"))
{
String sqlupdate2=("DELETE FROM Music WHERE Song =('"+song+"')");
stm.executeUpdate(sqlupdate2);
}
rst = stm.executeQuery("SELECT * from Music");
}
else
{
out.println( "Password is not correct!!!" );
}
out.println("insert attempted");
%>
</body>
</html>
I guess ur condition in if statement should be like this
if (action.equals("add")){.........................}
or
if (action == "add"){.........................}
instead of
if (request.getParameter("action").equals("add")){....................}
coz u catch option in action variable like this
String action = request.getParameter("option")

use get and make page url

i have this form:
<form action = "" method = "get">
<input type = "input" name = "id" value = "3" />
<input type = "input" name = "name" value = "gloris" />
<input type = "submit" class = "button_big" name = "submit" value = "SEND" />
</form>
And how make this link (and i must use button):
www.link.com/3/gloris
As Felix says, this requires JavaScript. It would be something like:
<script type="text/javascript">
window.addEventListener("load", function()
{
document.getElementById("myForm").addEventListener("submit", function()
{
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
window.location = [window.location.replace(/\/$/, ''), id, name].join("/");
}, false);
}, false);
</script>
<form action = "" id = "myForm" method = "get">
<input type = "text" name = "id" id = "id" value = "3" />
<input type = "text" name = "name" id = "name" value = "gloris" />
<input type = "submit" class = "button_big" name = "submit" id = "submit" value = "SEND" />
</form>
Note that I added id attributes so we can use document.getElementById. Also, there is no input type "input". It should be text, or you can leave it off. You can add more fields just be adding to the array in the desired order.
<form action="http://www.link.com/3/gloris" method="get">
<input type="submit" value="Go to gloris" />
</form>
I'm assuming you mean how do you make a link in the page the form is posted to? Depends on the technology you use. With php it could be
Your link name
If PHP is an option for you, have the form action link to code such as this:
<?php
if (isset($_GET['id']) && isset($_GET['name'])) {
header('Location: www.link.com/' . $_GET['id'] . '/' . $_GET['name']);
}
?>
Firstly, I would update your field names. Naming an item "name" is never a great idea. Also, add an id to your form.
<form action = "javscript:void();" method = "get" id = "mainForm">
<input type = "input" name = "itemId" value = "3" />
<input type = "input" name = "itemName" value = "gloris" />
<input type = "submit" class = "button_big" name = "submit" value = "SEND" />
</form>
<button onclick="updateLocation();">Submit</button>
Then just make a javascript function like so.
<script><!--
function updateLocation(){
var formObject=document.getElementById('formObject');
var i = formObject.itemId.value;
var n = formObject.itemName.value;
var url = 'www.link.com/' + i + '/' + n;
window.location=url;
}
--></script>