jsp部分:
<form method="post" action="http://localhost:8080/Demo/loginAction">
用户登录
</div>
<div style=" width:282px; height:42px; background-image:url(images/logindl.png);">
<input type="text" id="lname" name="username" style="font:16px; font-size:13px; width:282px; height:40px; float:right; border:none; background:none;" />
</div>
<div style=" width:282px; height:42px; background-image:url(images/loginmm.png); margin-top:10px;">
<input type="password" id="lpassword" name="password" style="font:16px; font-size:13px; width:282px; height:40px; float:right; border:none; background:none;" />
</div>
<div style="width:282px; height:43px; margin-top:16px;">
<input type="submit" value="登陆" style=" width:282px; height:43px;" class="button blue" />
<br />
<p style="font-size: 20px; color:red"><c:if test="${not empty sessionScope.msg}">
${sessionScope.msg}
<c:remove var="msg" scope="session"/>
</c:if></p>
</form>
java后台部分:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String path="login.properties";
String basePath =this.getClass().getResource("/").getPath();
path=basePath+path;
rprotety.load(path);
String mylog=rprotety.getProperty(username);
if(password.equals(mylog)){
request.getRequestDispatcher("/project/chartTwoTest.jsp").forward(request, response);
}
else {
System.out.println("用户名与密码错误");
request.getSession().setAttribute("msg","用户名或者登陆密码有误!");
response.sendRedirect(request.getContextPath()+"/AllLogin/login.jsp");
}
}
对properties操作的类:
public class readProperties {
private Properties prop = null;
public readProperties() {
prop = new Properties();
}
//读取相应的路径
public void load(String path) {
FileInputStream stream = null;
try {
stream = new FileInputStream(path);
prop.load(stream);
stream.close();
stream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
//给出key获取key对应的value值
public String getProperty(String key) {
return prop.getProperty(key);
}
//进行对property进行键值对的添加,有则修改,无则添加
public void addData(String key,String value,String path) throws Exception{
File file=new File(path);
if(!file.exists()){
file.createNewFile();
}
InputStream fis=new FileInputStream(file);
prop.load(fis);
fis.close();//一定要在修改值之前关闭fis
OutputStream fos = new FileOutputStream(path);
prop.setProperty(key, value);
prop.store(fos, path);
}
}