前端传输数据到后端
1.通过HttpServletRequest获取数据
此方法:request.getParameter("loginName"),loginName为input的name属性
前端代码
<form action="/chat" method="get"> <li> <input type="text" name="loginName" class="text" value="Username" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Username';}" ></input><a href="#" class=" icon user"></a> </li> <li> <input type="password" name="loginPassword" value="Password" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Password';}"></input><a href="#" class=" icon lock"></a> </li> <div class="p-container"> <label class="checkbox"><input type="checkbox" name="checkbox" checked="true"></input><i></i>Remember Me</label> <h6><a href="/account-reset">Forgot Password ?</a> </h6> <div class="clear"> </div> </div> <div class="submit"> <input type="submit" onclick="formSubmit()" value="SIGN IN" ></input> </div> <div class="social-icons"> <p>Or you can visit the following web</p> <ul class="soc_icons2"> <li class="pic"><a href="https://www.facebook.com/facebook"><i class="icon_4"></i></a></li> <li class="pic"><a href="https://twitter.com/"><i class="icon_5"></i></a></li> <li class="pic"><a href="https://cc.08800.top/webhp?gws_rd=cr"><i class="icon_6"></i></a></li> <div class="clear"></div> </ul> </div> <h5>Don't have an account ?<a href="/register"> Sign Up </a></h5> </form>
后端代码
public String login(HttpServletRequest request) { User user = new User(); user.setPhone(request.getParameter("loginName")); user.setName(request.getParameter("username")); user.setPassword(request.getParameter("password")); user.setEmail(request.getParameter("email")); implementChat.saveUser(user); System.out.println("LOGIN success"); return "login"; }