JSP 入门实例 及tomcat部署
2015-03-18 13:18
417 查看
使用MyEclipse创建一个web project
在WebRoot文件夹中新建两个页面login.html和returnMessage.jsp
login.html页面代码:
returnMessage.jsp代码:
src里新建一个包,包里两个Java文件:LoginValidate.java和LoginDBO.java
最后工程里的配置web.xml文件
修改Tomcat服务器中web.xml
把整个工程文件丢到tomcat/webapps里,然后启动tomcat,然后按路径可访问,比如http://192.168.0.13:8080/BBS/WebRoot/login.html
在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
相关文章推荐
- 动态网站入门(部署tomcat,了解jsp基础)
- 图解利用Eclipse3+Lomboz3+Tomcat开发JSP -- 4.一个Servlet实例
- (绝对原创)Step By Step 如何写出自己的第一个JSP+JavaBean+Tomcat的程序(本人亲身体验+实例教学)
- Tomcat_JSP经典配置实例
- Tomcat+JSP经典配置实例
- 配置Eclpise+tomcat与实现JSP部署
- 图解利用Eclipse3+Lomboz3+Tomcat开发JSP --2.编辑一个JSP实例
- 配置Eclpise+tomcat并实现JSP的编写与部署
- JSP实例入门(二) 键值对
- Tomcat+JSP经典配置实例
- Tomcat JSP经典配置实例
- 图解利用Eclipse3+Lomboz3+Tomcat开发JSP --2.编辑一个JSP实例
- 配置Eclpise+tomcat并实现JSP的编写与部署
- 配置Eclpise+tomcat并实现JSP的编写与部署
- Tomcat+JSP经典配置实例
- Tomcat 4/JSP入门指南
- 图解利用Eclipse3+Lomboz3+Tomcat开发JSP -- 4.一个Servlet实例
- 配置Eclpise+tomcat并实现JSP的编写与部署
- Tomcat+JSP经典配置实例
- Tomcat+JSP经典配置实例