<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
function sub(){
var id = $("#id").val();
var mm = $("#mm").val();
alert(id+mm);
/* $.ajax({
url:"login.action",
type:"post",
data:{id:id,mm:mm},
dataType:"text",
success:function (msg){
if(msg>0){
location="success.jsp";
}else{
location="error.jsp";
}
}
}); */
location ="login.action?id="+id+"&mm="+mm;
}
</script>
</head>
<body>
id:<input name="id" id="id"><br>
mm:<input name="mm" id="mm"><br>
<input type="button" value="登录" onclick="sub()">
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="abc" extends="struts-default">
<action name="login" class="com.action.Action">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
package com.action;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.service.CatService;
public class Action {
private CatService service = new CatService();
private String id;
private String mm;
public String execute() throws Exception{
int i = service.login(id,mm);
// HttpServletResponse response = ServletActionContext.getResponse();
// response.getWriter().print(i);
System.out.println(i);
if(i>0){
return "success";
}
return "error";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMm() {
return mm;
}
public void setMm(String mm) {
this.mm = mm;
}
}
package com.service;
import com.dao.CatDao;
public class CatService {
private CatDao dao = new CatDao();
public int login(String id, String mm) {
// TODO Auto-generated method stub
return dao.login(id,mm);
}
}
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class CatDao {
public int login(String id, String mm) {
// TODO Auto-generated method stub
Integer flag = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql:///cms04","root","root");
PreparedStatement pst = con.prepareStatement("select * from login where id='"+id+"' and mm='"+mm+"' ");
ResultSet rs = pst.executeQuery();
if(rs.next()){
flag++;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
“成败” 皆因 “性格” 所致
“性格” 却因 “环境” 而变
今生得失,时势使然,勿喜勿悲;