您的位置:首页 > 运维架构 > Tomcat

JSP 入门实例 及tomcat部署

2015-03-18 13:18 417 查看
使用MyEclipse创建一个web project

在WebRoot文件夹中新建两个页面login.html和returnMessage.jsp

login.html页面代码:

[code]<html>
  <head> 
    <title>登陆页面——中国网页设计</title>
  </head>

  <body> 
   <form method="post" action="login.do">
   用户名:
   <input type="text" name="username"/>
   密码:
  <input type="password" name="password"/>
  <input type="Submit" value="提交"/>
   </form> 
   </body>
</html>


returnMessage.jsp代码:

[code]<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" import="java.sql.*"%>
<html>
  <head>        
    <title>MVC(JSP+JavaBean+Servlet)入门实例--中国网页设计</title>
  </head>

  <body>
   <% 
     String message=new String ( request.getParameter("message").getBytes("ISO8859-1"),"GBK" ); %>
<%=message %>
  </body>
</html>


src里新建一个包,包里两个Java文件:LoginValidate.java和LoginDBO.java

[code]import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginValidate extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String name=request.getParameter("username");
  String password=request.getParameter("password");
  LoginDBO loginDBO=new LoginDBO();
  String message =loginDBO.login(name, password);
  String url="returnMessage.jsp?message="+message;
  url=new String(url.getBytes("GBK"),"ISO8859_1"); 
  response.sendRedirect(url); 
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

 this.doGet(request, response);
 }

}


[code]import java.sql.*;

public class LoginDBO {
 Connection conn;
 Statement stmt;
 public LoginDBO(){
  try{
   Class.forName("com.mysql.jdbc.Driver");
   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mvc_user","root","123");
   stmt=conn.createStatement();

  }catch(Exception e)
  {

   e.printStackTrace();
  }
 }
 public String login(String name, String password){
   String message="123";
   try{
    String sql1="select count(0) from user_info where user_name='"+name+"'";
    ResultSet rs1=stmt.executeQuery(sql1);
    if(rs1.next()){
     int userCount=rs1.getInt(1);
     if(0==userCount){
      message="不存在该用户";
      return message;
     }
     String sql2="select count(0) from user_info where user_name='"+name+"'and password='"+password+"'";
     ResultSet rs2=stmt.executeQuery(sql2);
     if(rs2.next()){
      int trueUserCount=rs2.getInt(1);
      if(0==trueUserCount){
       message="密码错误";
       return message;
      }
      message="成功!";
     }

    }
   }catch(Exception e)
   {
    e.printStackTrace();
   }
   return message;

  }

}


最后工程里的配置web.xml文件

[code]<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>包名.LoginValidate</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login.do</url-pattern>
  </servlet-mapping>


修改Tomcat服务器中web.xml

[code]    <init-param>
            <param-name>listings</param-name>
            <param-value>false(将其该为true)</param-value>
        </init-param>


把整个工程文件丢到tomcat/webapps里,然后启动tomcat,然后按路径可访问,比如http://192.168.0.13:8080/BBS/WebRoot/login.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: