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;
Related
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 am not able to display the value in drop down. I am getting the below error when I try to display the newbFileNetStatus in drop down.
Error:
javax.el.ELException: Cannot convert G of type class java.lang.String to class java.lang.Long
JSP page:
[smm [newbID=4, planID=174, applID=18, taxqID=1, newbPolicyNo=551732891, newbEffectiveDate=null, newbIssueDate=null, newbDeliveryEMail=Admin#***.com, newbCreatedBy=C983990, newbFormsComment=document bharathi msss, newbFileNetStatus=D]]
<div class="formcontent">
<form method="post" action="update">
<c:forEach items="${filenetStatus}" var="filenetStatus">
<input type="hidden" name="newbID" value="${filenetStatus.newbID}" />
<div class="testimonials">
<div class="left">
<label>Plan ID</label>
</div>
<div class="right">
<input name="planID" type="text" value="${filenetStatus.planID}" />
</div>
</div>
<div class="testimonials">
<div class="left">
<label>New Policy Number</label>
</div>
<div class="right">
<input name="newbPolicyNo" readonly="readonly" type="text"
value="${filenetStatus.newbPolicyNo}" />
</div>
</div>
<div class="testimonials">
<div class="left">
<label>New Form Comment</label>
</div>
<div class="right">
<input name="newbFormsComment" type="text"
value="${filenetStatus.newbFormsComment}" />
</div>
</div>
<div class="testimonials">
<div class="left">
<label>Created By</label>
</div>
<div class="right">
<input name="newbCreatedBy" readonly="readonly" type="text"
value="${filenetStatus.newbCreatedBy}" />
</div>
</div>
<div class="testimonials">
<div class="left">
<label>Delivery Email</label>
</div>
<div class="right">
<input name="newbDeliveryEMail" type="text"
value="${filenetStatus.newbDeliveryEMail}" />
</div>
</div>
<div class="testimonials">
<div class="left">
<label>Delivery Email</label>
</div>
<div class="right">
<select class="dropmenu"
name="newbFileNetStatus">
<c:choose>
<c:when test="${filenetStatus.newbFileNetStatus =='G'}">
<option value="D">D</option>
<option value="C">C</option>
<option value="H">H</option>
<option value="S">S</option>
<option value="R">R</option>
<option value="${filenetStatus.newbFileNetStatus}" selected>G</option>
</c:when>
</c:choose>
</select>
</div>
</div> -
I have a velocity template with tag form.
<div class="row">
<div class="container">
<div class="col-lg-12">
<table class=" table table-striped">
<thead>
<th>#</th>
<th>username</th>
<th>role</th>
<th>password</th>
</thead>
<tbody>
#foreach( $user in $users )
<tr>
<td>$user.id</td>
<td>$user.username</td>
<td>$user.role</td>
<td>$user.password</td>
<td>Delete</td>
<td>Edit</td></tr>
#end
</tbody>
</table>
<form method="post" action="/user">
<div class="form-group">
<label for="username">Username:</label>
<input type="username" class="form-control" id="username">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="form-group">
<label for="role">Role:</label>
<input type="role" class="form-control" id="role">
</div>
<button type="submit" class="btn btn-default">Add</button>
</form>
</div>
</div>
And I need to pass entered data to the controller. Here is controller code.
#RequestMapping(value = "/user", method = RequestMethod.POST)
public String addUser(#ModelAttribute User newUser) {
userService.save(newUser);
return "redirect:/users";
}
I'm newbie in velocity and I haven't figured out in this framework yet. I've google a long time but unsuccessful. Please help velocity guru!
The problem I see here is not just about velocity or Spring. Your form inputs does not have names, it seems you misplaced name for type in your form. It's the input names that's sent to the controller. What you want to do is create a User model and make sure it has the same variable names as the form input names.
public class User {
private String username;
private String password;
private String role;
// Add getter and setter methods
// Add toString method
}
And your form should be like
<form method="post" action="/user">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" name="password" class="form-control" id="pwd">
</div>
<div class="form-group">
<label for="role">Role:</label>
<input type="text" class="form-control" id="role" name="role">
</div>
<button type="submit" class="btn btn-default">Add</button>
</form>
Your controller method should get the user object like that. I just did a simple spring boot app to test and it worked.
I've created a JSP file for creating a form.
I'm trying to auto update or print data automatically into a text field in a form by uploading a .csv file using input type=file tag and then calculate the total price of items from the .csv file using JSP. That is I've to print the total price calculated from the .csv file onto the text field of the form.
Below is the JSP file.
Say the .csv file contains only 1 column containing prices of different items.
<%# 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>
<link rel="stylesheet" href="../CSS/bootstrap.css">
</head>
<body>
<div class="row">
<div class="container">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-area">
<form role="form" method="post" action="../AddDistributorDetail"> <br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Distributor Registration</h3>
<div class="form-group">
<label class="control-label">Distributor Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label class="control-label">Contact No #1:</label>
<input type="text" class="form-control" id="contact1" name="contact1" placeholder="Contact No1" required>
</div>
<div class="form-group">
<label class="control-label">Contact No #2:</label>
<input type="text" class="form-control" id="contact2" name="contact2" placeholder="Contact No2" required>
</div>
<div class="form-group">
<label class="control-label">Address #1:</label>
<textarea class="form-control" type="textarea" id="address1" placeholder="First Address" maxlength="200" rows="7"></textarea>
</div>
<div class="form-group">
<label class="control-label">Address #1:</label>
<textarea class="form-control" type="textarea" id="address2" placeholder="Second Address" maxlength="200" rows="7"></textarea>
</div>
<div class="form-group">
<label class="control-label">City:</label>
<input type="text" class="form-control" id="city" name="city" placeholder="City" required>
</div>
<div class="form-group">
<label class="control-label">Pincode:</label>
<input type="text" class="form-control" id="pin" name="pin" placeholder="Pincode" required>
</div>
<div class="form-group">
<label class="control-label">Zone:</label>
<div class="radio">
<label><input type="radio" name="zone">East</label>
</div>
<div class="radio">
<label><input type="radio" name="zone">South</label>
</div>
<div class="radio">
<label><input type="radio" name="zone">West</label>
</div>
<div class="radio">
<label><input type="radio" name="zone">North</label>
</div>
</div>
<div class="form-group">
<label class="control-label">STB Limit:</label>
<input type="text" class="form-control" id="stblimit" name="stblimit" placeholder="Set Top Box Limit" required>
</div>
<div class="form-group">
<label class="control-label">Credit Limit:</label>
<input type="text" class="form-control" id="climit" name="climit" placeholder="Max Credit Limit" required>
</div>
<div class="form-group">
<label class="control-label">Upload STB Inventory</label>
<input type="file" class=" file form-control" id="file_upload" name="file_upload" enctype="multipart/form-data" multiple required>
</div>
<div class="form-group">
<label class="control-label">Total Cost Of Inventory</label>
<input type="text" class="form-control" id="tcost" name="tcost" placeholder="Total Cost Of Inventory" required>
</div>
<button type="button" id="submit" name="submit" class="btn btn-primary">Submit Form</button>
</form>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
<script type="text/javascript" src="../JS/jquery.js"></script>
<script type="text/javascript" src="../JS/bootstrap.js"></script>
</body>
</html>
Even though I understood as how to read a csv file in JSP, I searched the internet but didn't find any logic as how to upload the file and calculate the total price in real time(i.e in the same JSP page and before clicking the submit button) and print it the text field in the form.
Any help regarding this will be much appreciated.
You can upload the file using ajax form submit. After uploading, in the server side just read the csv and writes the result back in the response. After receiving the result you can manipulate the dom element.
The easier way is to use the javascript "FileReader" api, which will be supported in most browser
<script type='text/javascript'>
var readFile = function(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
document.getElementById("myTextarea").value =reader.result;
};
reader.readAsText(input.files[0]);
};
</script>
<body>
<input type='file' onchange='readFile(event)'><br>
<textarea id='myTextarea'></textarea>
</body>
Js fiddle link : http://jsfiddle.net/gtxfye8m/
I've tested in chrome, edge.
You can create a Pojo class and declare all the fields which are present in the file and read the fields from your csv file and then set it into the respective objects that can be mapped to your jsp fields.
I am passing a value from a jsp page to bootstrap modal using jQuery and it is working. I want to pass the same value from bootstrap modal to the servlet. I tried with ajax, post, nothing seems to be working. Here is my html:
<div class="container" style="padding-top: 60px;" >
<select class="createAlarm" id="createAlarm" name="metrics">
<option value="cpuUsage">CPU Usage</option>
<option value="memoryUsage">Memory Usage</option>
<option value="diskRead">Disk Read</option>
<option value="diskWrite">Disk Write</option>
<option value="diskUsage">Disk Usage</option>
<option value="netUsage">Net Usage</option>
</select>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-whatever="Create Alarm">Create Alarm</button>
</div>
<!-- Modal- Create Alarm -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Create Alarm</h4>
</div>
<form action="/ProjectName/createAlarm" method="post" id="addcard" class="form-horizontal" role="form">
<div class="modal-body" style="height: 170px;">
<div class="form-group" style="height: 30px;">
<label for="title" class="col-md-3 control-label">Name</label>
<div class="col-md-9">
<input type="text" class="form-control" name="alarmName">
</div>
</div>
<div class="form-group" style="height: 30px; margin-bottom:30px;">
<label for="title" class="col-md-3 control-label">Description</label>
<div class="col-md-9">
<textarea class="form-control" name="desc"></textarea>
</div>
</div>
<div class="form-group">
<label for="priority" id="priority" name="priority" class="col-md-3 control-label">
<input type="text" class="form-control" name="name">
</label>
<div class="col-md-9">
<div class="col-md-3">
<select name="sign" class="form-control">
<option value="lessthan"><</option>
<option value="greaterthan">></option>
<option value="lessthanequalto"><=</option>
<option value="greaterthanequalto">>=</option>
</select>
</div>
<div class="col-md-3">
<input type="text" class="form-control" name="threshold">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-primary" id="formSubmit" type="submit">Create Alarm</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Here is my jQuery part:
<script>
$('#myModal').on('show.bs.modal', function(e) {
var metrics_key = $('.createAlarm').val();
var metrics_label = $(".createAlarm option:selected").text();
// Now you have the key and label in variables.
// Write the key into the textfield.
$('#myModal input[name="name"]').val(metrics_key);
// Change the HTML of an element to the label.
$('#myModal label[for="priority"]').text(metrics_label);
$.post("/CMPE283_Project2/createAlarm", { val : $("#priority").text()});
alert($('#priority').text());
});
My Servlet:
String Metric = request.getParameter("val");
But, I am getting null for the String Metric. But alert pop up after clicking 'Create Alarm' button shows the correct value I want to pass the servlet. Please help. Thanks.
Your post looks ok, I believe it's just a matter of jquery syntax for accessing controls.
In particular, I'd expect a text input with id="priority" (not just a label as it appears in your code), and then you access it with '.val()' and not 'text()'
<input type="text" id="priority" ... />
$.post('your url', {priority: $("#priority").val()} ...
At least this works for me (jquery version 2.1.3). If you want to make sure, you can just start by posting hard coded data such as {priority: '7'}