I have a Html page called kind.html inside the WEB-INF directory and an other jsp page called registration.jsp inside the WEB-INF folder. I need to put this registration.jsp page inside the WEB-INF directory so it cannot be accessible if a user attempts to get access to it by typing its URL. So my problem is how can i navigate from kind.html to registration.jsp with link called home I am newbie in this Thank you.
below is my code snippet and png file
kind.html..............................................
<li class='active'><a href='kind.html'><span>Home</span></a></li>
<li class='has-sub'><span>Register</span>
.............................registrationcontroller........................................
#RequestMapping(value="/registration",method = RequestMethod.POST)
public #ResponseBody
String firstRegistration(HttpServletRequest req,
HttpServletResponse response) {
response.setContentType("text/html");
RegistrationModel registrationModel = new RegistrationModel();
registrationModel.setFirstName(req.getParameter("first_name"));
System.out.println("controller " + req.getParameter("first_name") );
registrationModel.setLastName(req.getParameter("last_name"));
registrationModel.setPassword(req.getParameter("password"));
registrationModel.setEmailID(req.getParameter("email"));
System.out.println("controller email " + req.getParameter("email"));
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
try {
Date date = format.parse(req.getParameter("BirthDate"));
registrationModel.setDOB(date);
} catch (Exception e) {
e.printStackTrace();
}
String phoneno=req.getParameter("phoneNo");
Integer phoneNo = Integer.parseInt(phoneno);
System.out.println("phone no ...."+phoneNo);
registrationModel.setPhoneNo(phoneNo);
registrationModel.setGender(req.getParameter("gender"));
String age=req.getParameter("Age");
Long AGE = Long.parseLong(age);
registrationModel.setAge(AGE);
registrationModel.setAvtar(req.getParameter("Avtar"));
System.out.println("avtar"+ req.getParameter("Avtar"));
Address address = new Address();
address.setAddressline(req.getParameter("Full-Address"));
address.setCity(req.getParameter("city"));
address.setLandmark(req.getParameter("landmark"));
address.setState(req.getParameter("state"));
String zipCode =req.getParameter("Zipcode");
Long zipcode = Long.parseLong(zipCode);
address.setZipcode(zipcode);
registrationModel.setAddress(address);
registrationService.resgistration(registrationModel);
return "registration.jsp";
}
..........................registration.jsp.........................................................
<form action="registration" method="post">
<fieldset>
<legend>Register Form</legend>
<div>
<input type="text" name="first_name" placeholder="First Name" />
</div>
<div>
<input type="text" name="last_name" placeholder="Last Name" />
</div>
<div>
<input type="password" name="password" placeholder="Password" />
</div>
<div>
<input type="text" name="email" placeholder="Email" />
</div>
<div>
<input type="text" name="BirthDate" placeholder="BirthDate" />
</div>
<div>
<input type="number" name="Age" placeholder="Age" />
</div>
<div>
<select name="gender">
<option value="select">i am..</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select><br> <br>
</div>
<div>
<input type="number" name="phoneNo" placeholder="PhoneNo" />
</div>
<div>
<input type="text" name="Full-Address"
placeholder="Full-Address" />
</div>
<div>
<input type="text" name="landmark" placeholder="landmark" />
</div>
<div>
<input type="text" name="city" placeholder="city" />
</div>
<div>
<input type="text" name="state" placeholder="state" />
</div>
<div>
<input type="number" name="Zipcode" placeholder="Zipcode" />
</div>
<div>
<input type="file" name="Avtar" placeholder="Avtar" />
</div>
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>![folder structure of project][2]
1st to call your pages html is static so you can use mvc resources "" for it, for jsp you have to use ViewResolver for jstl use InternalResourceViewResolver in its configuration as :
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
then in your controller use
return "registration";
also you will need method to handle GET request and change below line
<li class='has-sub'><span>Register</span>
if you are using Spring boot you will find these configuration automated.
Related
I'm trying to upload a file to cloudinary. I'm stucked at how to only get parts of image from the form. It keeps on throwing exception: Invalid image file. If I remove all text inputs in the form, the uploading is successful. I guess that happens because the form also has text inside. Please help me solve this. I'm really grateful for your support.
Here is my code:
Form.jsp:
<form role="form" action="<c:url value="/admin/product/update"/>" method="post" enctype="multipart/form-data">
<input name="id" value="${product.id}" hidden="">
<div class="form-group">
<label>Name:</label> <input class="form-control" value="${product.name}" name="name" />
</div>
<div class="form-group">
<label>Price:</label> <input class="form-control" value="${product.price}" type="number" name="price" />
</div>
<div class="form-group">
<label>Quantity:</label> <input class="form-control" value="${product.quantity}" type="number" name="quantity" />
</div>
<div class="form-group">
<label>Image:</label> <input class="form-control" value="${product.image}" name="image" />
</div>
<div class="form-group">
<label>Description </label> <br>
<textarea rows="4" cols="50" name="description" value="${product.description}" ></textarea>
</div>
<div class="form-group">
<label>Category</label>
<div class="checkbox">
<select name="catid">
<c:forEach items="${categorylist}" var="c">
<option value="${c.id}">${c.name}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label>image</label> <input type="file" name="image" value="${product.image }" />
</div>
Servlet.java
BeanUtils.populate(product, request.getParameterMap());
//if (catid != product.getCategory().getId()) {
// Category category = new Category();
category = dao2.getCategoryByID(catid);
product.setCategory(category);
Map result = null;
Collection<Part> fileParts = request.getParts();
for (Part part : fileParts) {
String fileName = part.getSubmittedFileName();
result = UploadImage.uploadImage(fileName, part);
String url = String.valueOf(result.get("url"));
product.setImage(url);
if (result == null) {
throw new RuntimeException("Loi upload");
}
}
dao.update(product);
The Cloudinary upload method supports uploading media files from the sources like a local path, a remote URL, a private storage URL (S3 or Google Cloud storage), a base64 data URI, or an FTP URL.
Based on your code, it seems that you are only supplying the filename of the image.
String fileName = part.getSubmittedFileName();
result = UploadImage.uploadImage(fileName, part);
You would need to update the code to input the local path of the image.
Cuba has its own backend system to add users.
Now I need to write a user registration in the front-end.
The Cuba version I am using is 6.9
I know that this version of encryption is SHA1 : https://doc.cuba-platform.com/manual-6.9/login.html
Now my question is : I don't know how to set the encrypted password to the database.
I create an entity through the Metadata
User user = metadata.create(User.class);
user.setPassword(passWord);
I'm not sure that's the best option but I used the following code:
#Inject
protected PasswordEncryption passwordEncryption;
...
user.setPassword(passwordEncryption.getPasswordHash(user.getId(), password));
I think you just need to create html file with this code:
<div class="span6">
<h3>Login</h3>
<div th:if="${(param.error != null)}">
<p>Invalid username / password</p>
</div>
<form id="f" name="f" method="POST" action="login">
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<div>
<div>
<label style="width: 80px" for="login">Login</label>
<input type="text" id="login" name="login" value=""/>
</div>
<div>
<label style="width: 80px" for="password">Password</label>
<input type="password" id="password" name="password" value=""/>
</div>
</div>
<button type="submit">Login</button>
</form>
</div>
and then Request it from Controller:
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
if (PortalSessionProvider.getUserSession().isAuthenticated()) {
LoadContext l = new LoadContext(User.class);
l.setQueryString("select u from sec$User u");
model.addAttribute("users", dataService.loadList(l));
}
return "index";
}
I am sending text values from jsp to servlet but in servlet request.getParameter() gives me null values. Below I am mentioning my JSP, Servlet and web.xml
REGISTER.JSP
<form id="contactform" action="servlet/SaveInfo" method="post">
<p class="contact"><label for="email">Email</label></p>
<input id="email" name="email" placeholder="example#domain.com" required="" tabindex="1" type="email">
<p class="contact"><label for="username">User Name</label></p>
<input id="username" name="username" placeholder="username" required="" tabindex="2" type="text">
<p class="contact"><label for="password">Password</label></p>
<input type="password" id="password" name="password" required="" tabindex="3">
<p class="contact"><label for="repassword">Confirm Password</label></p>
<input type="password" id="repassword" name="repassword" required="">
<p class="contact"><label for="phone">Mobile Phone</label></p>
<input id="phone" name="phone" placeholder="phone number" required="" tabindex="4" type="text"> <br>
<p class="contact"><label for="name">Organization Name</label></p>
<input id="Orgname" name="name" placeholder="Organization name" required="" tabindex="5" type="text">
<p class="contact"><label for="Address">Address</label></p>
<input id="address" name="name" placeholder="Street,Area,city" required="" tabindex="6" type="text"> <br>
<!-- <select class="select-style" name="BirthMonth"> -->
<label>State</label><br>
<select class="select-style" name="BirthMonth">
<%
ResultSet rs = null;
Connection con = null;
PreparedStatement pmst1= null;
int countryid = 208;
try{
con = DBConnectivity.getOracleDBConnection();
pmst1 = con.prepareStatement(Sqlquery.getRegion());
pmst1.setInt(1, countryid);
rs=pmst1.executeQuery();
while(rs.next()){
String name = rs.getString("name");
%>
<option value="<%=name %>"><%=name %></option>
<% } %>
<%
}catch(Exception e){
}
%>
</select><br><br>
<p class="contact"> <input id="check" name="name" style="margin-right: 10px" type="checkbox">
<label for="name"> Not Yet Registered GSTIN</label> </p>
<p class= "GSTIN"><label for="GSTIN">GSTIN No.</label></p><br>
<input id = "GSTINNO" maxlength="15" name= "GSTIN" placeholder="GSTIN No." type ="text"><br><br>
<input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">
SaveInfo.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
ResultSet rs = null;
Connection con = null;
PreparedStatement pmst1= null;
String Orgname = request.getParameter("Orgname");
String check =request.getParameter("check");
String GSTINNO = request.getParameter("GSTINNO");
con = DBConnectivity.getOracleDBConnection();
int clientid = 1000014;
MClient Client = new MClient (Env.getCtx(), clientid, null);
I want all the values in my servlet class and save that values in database below I am mentioning my web.xml file
Kindly help me out
<servlet>
<description></description>
<display-name>SaveInfo</display-name>
<servlet-name>SaveInfo</servlet-name>
<servlet-class>com.org.register.SaveInfo</servlet-class>
<servlet-mapping>
<servlet-name>SaveInfo</servlet-name>
<url-pattern>/servlet/SaveInfo</url-pattern>
</servlet-mapping>
In Servlet you need to get parameters by name attribute, not id.
For example, You have the following,
<input id="Orgname" name="name" placeholder="Organization name" required="" tabindex="5" type="text">
Here you have given id to be Orgname and name parameter is name. So in Servlet you will do,
request.getParameter("name");
But you are doing,
String Orgname = request.getParameter("Orgname");
Secondly, you cannot have same name for two parameters. For both the following you have given name to be name
<input id="Orgname" name="name" placeholder="Organization name" required="" tabindex="5" type="text">
<input id="address" name="name" placeholder="Street,Area,city" required="" tabindex="6" type="text"> <br>
Give some different name to the parameters.
The getParameter function of request object works with name attribute. So please replace your jsp with following.
<form id="contactform" action="servlet/SaveInfo" method="post">
<p class="contact"><label for="email">Email</label></p>
<input id="email" name="email" placeholder="example#domain.com" required="" tabindex="1" type="email">
<p class="contact"><label for="username">User Name</label></p>
<input id="username" name="username" placeholder="username" required="" tabindex="2" type="text">
<p class="contact"><label for="password">Password</label></p>
<input type="password" id="password" name="password" required="" tabindex="3">
<p class="contact"><label for="repassword">Confirm Password</label></p>
<input type="password" id="repassword" name="repassword" required="">
<p class="contact"><label for="phone">Mobile Phone</label></p>
<input id="phone" name="phone" placeholder="phone number" required="" tabindex="4" type="text"> <br>
<p class="contact"><label for="name">Organization Name</label></p>
<input id="Orgname" name="Orgname" placeholder="Organization name" required="" tabindex="5" type="text">
<p class="contact"><label for="Address">Address</label></p>
<input id="address" name="address" placeholder="Street,Area,city" required="" tabindex="6" type="text"> <br>
<!-- <select class="select-style" name="BirthMonth"> -->
<label>State</label><br>
<select class="select-style" name="BirthMonth">
<%
ResultSet rs = null;
Connection con = null;
PreparedStatement pmst1= null;
int countryid = 208;
try{
con = DBConnectivity.getOracleDBConnection();
pmst1 = con.prepareStatement(Sqlquery.getRegion());
pmst1.setInt(1, countryid);
rs=pmst1.executeQuery();
while(rs.next()){
String name = rs.getString("name");
%>
<option value="<%=name %>"><%=name %></option>
<% } %>
<%
}catch(Exception e){
}
%>
</select><br><br>
<p class="contact"> <input id="check" name="name" style="margin-right: 10px" type="checkbox">
<label for="name"> Not Yet Registered GSTIN</label> </p>
<p class= "GSTIN"><label for="GSTIN">GSTIN No.</label></p><br>
<input id = "GSTINNO" maxlength="15" name= "GSTIN" placeholder="GSTIN No." type ="text"><br><br>
<input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">
JSP post parameters using their attribute name. you wrote the wrong attribute name in 3 parameters.
Change:
<input id="check" name="name"
<input id="Orgname" name="name"
<input id="address" name="name"
To:
<input id="check" name="check"
<input id="Orgname" name="Orgname"
<input id="address" name="address"
I inherited a spring 4 web project at work which is configured with Tiles view framework, Spring framework for dispatcher servlet and IOC.
I am trying to build a reset password functionality which has three screens with three forms.
1)reset form. Takes the user's email and controller method sends an email with a temp password.
<form:form class="form-horizontal" action="/NCHP/reset.htm" method="post" id="resetForm">
<fieldset>
<legend>Enter your registered email address</legend>
<div class="form-group">
<div class="col-sm-12">
<input id="resetEmail" name="email" class="form-control" placeholder="Email" required="required" tabindex="1" type="email">
</div>
</div>
<div class="form-group">
<div class="col-sm-12 ">
<span class="pull-right">
<button type="reset" class="btn btn-default" data-dismiss="modal">Back</button>
<button type="submit" class="btn btn-primary" >Submit</button>
</span>
</div>
</div>
</fieldset>
</form:form>
2) Form to enter temp pwd:
<form:form class="form-horizontal" name="verify" method="POST" id='verifyform'>
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-user form-control-feedback"></i>
<input type="text" id="inputEmailVerify" name="userNameVerify" placeholder="User Name" class="form-control" autofocus="autofocus">
</div>
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-lock form-control-feedback"></i>
<input type="password" name="passwordVerify" id="inputPasswordVerify" placeholder="Password" class="form-control" autofocus="autofocus">
</div>
<div class="form-group" id="verify-btn">
<div class="col-sm-12 ">
<span class="pull-right">
<input type="submit" formaction="/NCHP/resetVerify.htm" value="Verify" class="btn btn-primary btn-block">
</span>
</div>
</div>
</form:form>
3) form to enter new password:
<form:form class="form-horizontal" name="resetPass" method="POST" id='resetPassForm' >
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-lock form-control-feedback"></i>
<input type="password" id="newpass" name="newpass" placeholder="new Password" class="form-control" autofocus="autofocus">
</div>
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-lock form-control-feedback"></i>
<input type="password" name="newpass2" id="newpass2" placeholder="re enter Password" class="form-control" autofocus="autofocus">
</div>
<div class="form-group" id="new-login-btn">
<div class="col-sm-12 ">
<span class="pull-right">
<input type="submit" formaction="/NCHP/changePass.htm" value="Save Password" onsubmit="modaljay();" class="btn btn-info btn-block">
<div class="modaljay"><!-- Place at bottom of page --></div>
</span>
</div>
</div>
</form:form>
The first two forms i do an ajax submit and get back just the data without a view like below with jquery $.ajax()
$('#resetForm').submit(function(event) {
$("#verifyModal").hide();
$("passModal").hide();
var resetmail = $('#resetEmail').val();
console.log(resetmail);
var json = {
"email" : resetmail
};
$.ajax({
url : "http://localhost:8080/NCHP/reset.htm",
data : json,
dataType : 'text json',
type : "POST",
success : function(e) {
$('#resetModal').modal('hide');
$("#verifyModal").modal('show');
console.log(e);
},
error : function(e) {
console.log(e);
if (e.responseText == "email sent") {
$('#resetModal').modal('hide');
$("#verifyModal").modal('show');
} else if (e.responseText == "email not sent. UNAUTHORIZED") {
$("#emailResponce").html("there was an error. We could not process your request at this time. Please contact support#nexiscard.com");
}
}
});
event.preventDefault();
});
The third form is a regular form submission and no ajax submission. because i want the user to be forwarded to their home page after they login with temp and set new password.
Below are the controller mappings for first two forms.
#Controller
public class UserLoginreset
{
#Autowired
public UserDao userDao;
#Autowired
public BCryptPasswordEncoder encoder;
#Autowired
public SecureServiceClient serviceClient;
#RequestMapping(value = "/reset", method = RequestMethod.POST)
public #ResponseBody String reset( #RequestBody(required=true) String email,
HttpServletRequest request)
{
String mail="";
try{
mail = java.net.URLDecoder.decode(email, "UTF-8").replace("email=", "");
}
catch (UnsupportedEncodingException e){
e.printStackTrace();
}
User user =userDao.findUserByemail(mail);
request.getSession().setAttribute("user",user);
String resetPwd= encoder.encode("xxxx");
if(user!=null && email!=null){
userDao.resetPass(user.getUser_name(), resetPwd);
return (String) serviceClient.returnRestTemplate("email", mail);//"home";
} else {
return "login";//"index";
}
}
#RequestMapping(value = "/resetVerify", method = RequestMethod.POST)
public #ResponseBody String resetVerify( #RequestParam(value="user",required=true) String user,#RequestParam(value="pass",required=true) String pass,
HttpServletRequest request)
{
User user1 =userDao.findUserByName(user);
if(user1!=null && pass.matches("xxxx")){
return "success";//"home";
} else {
return "unauthorised";//"index";
}
}
}
and third form.
#RequestMapping(value = "/changePass", method = RequestMethod.POST)
public #ResponseBody String resetPass( #RequestParam(value="newpass", required=true) String newpass, #RequestParam(value="newpass2", required=true) String newpass2,
HttpServletRequest request, Model model, HttpServletResponse httpServletResponse)
{
User user = (User)request.getSession(false).getAttribute("user");
if(user.getUser_name()!=null && newpass.matches(newpass2)){
userDao.resetPass(user.getUser_name(), encoder.encode(newpass));
String news = userDao.getDynamicNewsByUser(user.getUser_name());
model.addAttribute("user",user);
model.addAttribute("news", news);
return "MainLayout_Jay";//"home";
} else {
return "login";//"index";
}
}
And below is my spring config
<bean id="annotationResolver" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.nexis.cardholder" />
<mvc:annotation-driven >
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="com.nexis.cardholder.session.interceptors.URLInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/Pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="1" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" >
<property name="order" value="2" />
</bean>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="0" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views.xml</value>
</list>
</property>
</bean>
I have two issues.
1) The last form submission dosent return a view with the data in it. It just returns an empty page with controller method's return string in it.
2) I configured my home page in both tiles.xml and put it in /webcontent/pages folder. so if tiles view resolver could not find it, regular resolver should have. i doubt if its treating this request as a ajax request and skipping the model-view mapping part totally. how do i figure out the root cause.
2.5)if i want to parse ajax requests as Json, should i use #restcontroller and will it automatically send the responce as json? i tried using MappingJackson2HttpMessageConverter but didnt see any difference in debug mode?? did it even get used?
Got it. It didnt take as much time as typing this question.
i needed to remove the #ResponseBody annotation at
public #ResponseBody String resetPass(
I need send a form from php to java using the method post and curl.
I'm having problems to fetch a list of objects in php when I submit the form. I would like to know if is possible to do it like in Spring MVC (specifying attribute name in the form)
<form method="post" action="group_create.php">
<div>
Name: <input type="text" name="name" />
<br />
<input type="text" name="users[0].name" value="753" />
<br />
<input type="text" name="users[1].name" value="752" />
<br />
<input type="submit" value="Save" />
</div>
</form>
Encoding method:
<?php
http_build_query($_POST);
?>
Current curl url:
javaserver/app/groups/create?name=Administrator&users[0]=Tom&users[1]=Myke
Necessary curl url:
javaserver/app/groups/create?name=Administrator&users[0].name=Tom&users[1].name=Myke
My DTO's:
public class GroupDTO {
private String name;
private List<UserDTO> users;
public GroupDTO()
{
this.users = new ArrayList<UserDTO>();
}
}
public class UserDTO {
private String name;
}
Spring MVC Controller:
#Controller
#RequestMapping("secure/groups")
public class GroupsController {
#RequestMapping(value = "create", method = RequestMethod.POST)
public #ResponseBody ModelMap create(GroupDTO dto)
{
ModelMap map = new ModelMap();
//CODE TO DO ANYTHING
return map;
}
}
Change this:
<form method="post" action="group_create.php">
<div>
Name: <input type="text" name="name" />
<br />
<input type="text" name="users[0].name" value="753" />
<br />
<input type="text" name="users[1].name" value="752" />
<br />
<input type="submit" value="Save" />
</div>
</form>
To this:
<form method="post" action="group_create.php">
<div>
Name: <input type="text" name="name" />
<br />
<input type="text" name="users[0]" value="753" />
<br />
<input type="text" name="users[1]" value="752" />
<br />
<input type="submit" value="Save" />
</div>
</form>
The ".name" part is not needed for the form to operate properly.