I have registration form jsp page that calls a servlet to add data to mysql database. In the same page I have 2 dropdowns, First one is country which is hard-coded.
According to the selected value, second combobox should populate with the appropriate city_name values before submit the form.
But I can't use php here.
table1-->user(fname,lname,country,city_id)
table2-->city(city_id,city_name,country)
HTML,
<form role="form" name="form1" action="Registration" method="post" class="signup-form">
<div class="form-group">
<select
onchange="document.getElementById('country').value=this.options[this.selectedIndex].text; document.getElementById('idValue').value=this.options[this.selectedIndex].value;">
<option></option>
<option value="sl">Sri Lanka</option>
<option value="jp">Japan</option>
<option value="en">England</option>
<option value="kr">Korea</option>
<option value="us">USA</option>
</select>
<input name="country" placeholder="Select Your Country" id="country" onfocus="this.select()" type="text" required>
</div>
<div class="col-md-4">
<select id="city" name="city" class="form-control">
<option>--Select--</option>
</select>
<label id="city1" class="input-group-error form-error"></label>
</div>
</form>
Related
I'm working on small project where I'm trying to dd profile so the user can upload his profile pic so while I was searching for references I found some Youtube videos and I started coding and I want to link the image which user uploaded as his profile image.
There I'm getting the error "The type Part is ambiguous"(in jsp)
<%#page
import="java.sql.*,java.text.*,javax.servlet.*,java.security.MessageDigest,
java.security.NoSuchAlgorithmException
,javax.mail.*,javax.mail.internet.*,
java.io.File,java.io.PrintWriter,java.awt.List"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%!
%>
<%
String username=(String) session.getAttribute("nkey");
String f_name =request.getParameter("f_name");
String l_name =request.getParameter("l_name");
Part part =request.getPart("profile_link");(here I'm getting the issue)
String id_link =request.getParameter("id_link");
String age =request.getParameter("age");
String gender =request.getParameter("gender");
String address =request.getParameter("address");
String pincode =request.getParameter("pincode");
String qualification =request.getParameter("qualification");
String year =request.getParameter("year");
String college_name =request.getParameter("college_name");
String state =request.getParameter("state");
String country =request.getParameter("country");
%>
<%!
private static final String SAVE_DIR="Pictures";
%>
<%response.setContentType("text/html;charset=UTF-8");%>
<%
PrintWriter outt=response.getWriter();
String savePath="C:/Users/thots/Desktop/My World/internsip/Student_Portal 2.0/WebContent"+File.separator+SAVE_DIR;
File fileSaveDir=new File(savePath);
String fileName=extractFileName(part);
part.write(savePath + File.separator + fileName);
String filePath=savePath+File.separator + fileName;
%>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Student_Portal 2.0","root","");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into student_details values('"+username+"','"+f_name+"','"+l_name+"','"+filePath+"','"+id_link+"','"+age+"','"+gender+"','"+address+"','"+pincode+"','"+qualification+"','"+year+"','"+college_name+"','"+state+"','"+country+"')");
if(i>0)
{
response.sendRedirect("index.jsp");
Statement stt=con.createStatement();
String sql="update user_login set P_Status='1' where username='"+username+"'";
stt.executeUpdate(sql);
}
else
{
System.out.println("Failed");
}
}
catch (Exception e)
{
e.printStackTrace();
}
%>
Html code
<form action="add_student_details.jsp" enctype="multipart/form-data" method="post">
<div class="container-fluid">
<div>
<label for="img">Upload Profile Picture</label>
<input name="profile_link" type="file" class="form-control" required>
</div>
<br>
<div>
<label for="img">Upload Your ID card</label>
<input name="id_link" type="file" class="form-control" required>
</div>
<br>
<div class="row">
<div class="col-6">
<input name="f_name" type="text" class="form-control" placeholder="First name" required>
</div>
<div class="col-6">
<input name="l_name" type="text" class="form-control" placeholder="Last name" required>
</div>
</div>
<br>
<div class="row">
<div class="col-6">
<input name="age" type="number" class="form-control" placeholder="Age" required>
</div>
<div class="col-6">
<select name="gender" class="form-control" required>
<option>Male</option>
<option >Female</option>
</select>
</div>
</div>
<br>
<div>
<textarea name="address" rows="4" cols="55" placeholder="Your Address" id="w3review" required></textarea>
</div>
<br>
<div>
<input name="pincode" type="number" class="form-control" placeholder="Pincode" required>
</div>
</div>
<br>
<hr>
<div class="modal-body">
<h4>Educational Info</h4>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-6">
<select name="qualification" class="form-control" required>
<option selected disabled>Select Your Qualification</option>
<option>B.Tech</option>
<option >B.E</option>
</select>
</div>
<div class="col-6">
<select name="year" class="form-control" required>
<option selected disabled>Select Your Year</option>
<option>I</option>
<option >II</option>
<option>III</option>
<option >IV</option>
</select>
</div>
</div>
<br>
<div >
<select name="college_name" class="form-control" required>
<option selected disabled>Select Your College</option>
<option>ABCD College Of Engineering And Technology</option>
<option >XYZ College Of Engineering And Technology</option>
</select>
</div>
<br>
<div class="row">
<div class="col-6">
<select name="state" class="form-control" required>
<option selected disabled>Select Your State</option>
<option>Telangana</option>
<option >Kerala</option>
</select>
</div>
<div class="col-6">
<select name="country" class="form-control" required>
<option selected disabled>Select Your Country</option>
<option>India</option>
<option>United States</option>
</select>
</div>
</div>
<br>
<hr>
<center>
<button type="submit" class="submit-btn">Submit</button>
</center>
</div>
<br>
</form>
Sorry for sharing the whole code.
i tried multiple ways but none worked for me
someone please help me
Thank you.
The error means that the compiler can resolve the type Part to more than one class available in the packages you're importing (or a name conflict with a one of your custom classes that also happen to be named Part).
In your specific case, you're importing two packages, each of which includes a class named Part.
javax.servlet.http.Part
javax.mail.Part
To avoid this error, you can use the full namespace for the class like this:
javax.servlet.http.Part part =request.getPart("profile_link");
Best practice, however, is to avoid importing entire packages when you're only using specific classes from those packages whenever possible.
Do not:
import java.servlet.*;
Do:
import java.servlet.http.Part;
I'm a beginner in Spring and I'm trying to fix some small problem in my application. I have a form to edit the user profile. In this form, I have one dropdown list where admin user can select user role for the user that is being edited. When I'm going to edit user page then all data in form are populated from the database. Let's say I have two inputs in the form: input with lastName and select with user role. The select element contains all the roles that exist in the database but the selected role is matched to the role that the user actually have assigned.
This is part of my form - input lastName
<spring:bind path="lastName">
<div class="form-group">
<label for="lastName">Last name</label>
<form:input path="lastName" type="text" class="form-control ${status.error ? 'border border-danger' : ''}" id="lastName" />
<form:errors path="lastName" cssStyle="color: #ff0000;"/>
</div>
</spring:bind>
and this is a select element:
<form:select path="roles" id="role" multiple="false">
<form:options items="${allRoles}" itemValue="id" itemLabel="role"/>
</form:select>
Now, when I want to edit lastName eg. leave the blank field, then spring validates this field and throws an error. The problem is that role that was previously selected in select element is now unselected.
This is page HTML before validation:
input:
<div class="form-group ">
<label for="email" class="active">Email</label>
<input id="email" name="email" type="email" class="form-control" value="ola#test.com">
</div>
select:
<select id="role" name="roles"">
<option value="1">ADMIN</option>
<option value="2">USER</option>
<option value="3">STUDENT</option>
<option value="4" selected="selected">LECTURER</option>
</select>
and once lastName field is cleared and form submited:
input:
<div class="form-group">
<label for="lastName" class="">Last name</label>
<input id="lastName" name="lastName" type="text" class="form-control border border-danger" value="">
<span id="lastName.errors" style="color: #ff0000;">This field is required.</span>
</div>
select:
<div class="form-group">
<label for="role">Role Id</label>
<select id="role" name="roles">
<option value="1">ADMIN</option>
<option value="2">USER</option>
<option value="3">STUDENT</option>
<option value="4">LECTURER</option>
</select>
</div>
As you can see selected= "selected" attribute has disappeared from option 4. How to prevent this? Btw. I'm aware of this Spring MVC selected value in form:selected after form validation error
but it seems that this isn't work in my case.
I found some tip in archived page here
Now selected attribute is not removed from the option tag. I'm not sure if this is good way to solve this, but it works for me..
Modified select:
<form:form method="post" modelAttribute="editForm">
<c:forEach items="${editForm.roles}" var="role">
<c:set var="userRoleId" value="${role.id}" scope="request"/>
</c:forEach>
<form:select path="roles" id="role" multiple="false">
<c:forEach var="tempRole" items="${allRoles}">
<option value="${tempRole.id}" <c:if test="${tempRole.id == userRoleId}">selected="selected"</c:if> value="${tempRole.id}">${tempRole.role}</option>
</c:forEach>
</form:select>
</form:form>
I have a <div> tag:
<div id="content">
<jsp:include page="some.jsp"/>
</div>
Also I have a few buttons:
<input type="submit" name="btn1" value="Page1">
<input type="submit" name="btn2" value="Page2">
I need when I click btn1 some.jsp changes to page1.jsp and to page2.jsp when click btn2.
Use dynamic include,
<jsp:include page="<%= myVariable %>" flush="true" />
Pd: Take a look a flush http://www.coderanch.com/t/484149/JSP/java/flush-true-jsp-include-tag.
1- Instead of using static include, you can use dynamic include, then you can do something like this:
<jsp:include page="${somePage}" flush="true" />
2- Use javascript to change the action of the form depending on the button you click:
<input type="submit" value="Page1" name="btn1"
onclick="document.forms[0].action = 'somePage.jsp'; return true;" />
Im using this solution : My form is :
<FORM>
<select name="choix">
<option value="choix 1">choix 1</option>
<option value="choix 2">choix 2</option>
<option value="choix 3">choix 3</option>
</select>
<input type="submit" />
</FORM>
and im using this in the same jsp page to include a page as what i select in that form:
<%
Ch = request.getParameter("choix");
%>
<div id="inculde_page">
<jsp:include page="<%= "layouts/" + Ch + ".jsp" %>" />
</div>
I am new to JSP, and have a question as below.
In servlet1, I can use request.getParameter() to get the value to SELECT var1.
String ciStr = request.getParameter("var1")
But is there any way I can get the SELECT var1 from servlet2?
Thanks.
JSP code
<form action="Servlet1" method="post" enctype="multipart/form-data">
Confidence Interval
<SELECT name="var1" >
<OPTION value="ci99">99%</OPTION>
<OPTION value="ci95">95%</OPTION>
<OPTION value="ci90">90%</OPTION>
</SELECT> <br>
<textarea name="textArea1" style="width:500px;height:150px;"></textarea> <br>
<input type="submit" value="Submit" size="20" />
</form>
<form action="Servlet2" method="post" enctype="multipart/form-data">
Confidence Interval
<SELECT name="var2" >
<OPTION value="ci99">99%</OPTION>
<OPTION value="ci95">95%</OPTION>
<OPTION value="ci90">90%</OPTION>
</SELECT> <br>
<textarea name=textArea2" style="width:500px;height:150px;"></textarea> <br>
<input type="submit" value="Submit" size="20" />
</form>
Yes, You can use HttpSession object to set the value over server and then get these value any where on server.
HttpSession hs=request.getSession();
and you can set the value with a unique name .
hs.setAttribute("name","value");
and get any where on server by use the name.
String var=(String)hs.getAttribute("name");
Trying to submit checked selectbox value(on at a time) along with textbox value to a servlet;here is my code(jsp) :
<script type="text/javascript">
function search(){
document.f2.action="/InfoUser/SearchBox";
document.f2.submit();
}
</script>
<form name="f2">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> ----------- </option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search" onclick="search()">
</div>
</form>
and in the servlet : I have written
String[] select = request.getParameterValues("select");
String search = request.getParameter("search");
Unable to fetch the value in servlet,am I missing some javascript/jquery script to get the value in servlet & how I can use it with the help of JSTL.Any rectification if I'm going wrong .....feel free to comment,will be welcome.
Try to change your form:
<form action="/InfoUser/SearchBox">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> -----------</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search">
</div>
</form>
Or:
<script type="text/javascript">
function search(){
document.f2.action="/InfoUser/SearchBox";
document.f2.submit();
}
</script>
<form name="f2">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> ----------- </option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search" onclick="search()">
</div>
</form>
You are missing the name of the form and in your javascript you are trying to set the action on non-existing form.
In servlet you have to do:
String select = request.getParameter("select");
String search = request.getParameter("search");
You won't be able to use: String[] select = request.getParameterValues("select"); unless your select element will have multiple="true".