jsp环境搭建,入门示例程序

很久没写jsp了,今天心血来潮,想搞一搞jsp。

首先安装完MyEclipse,新建一个工程helloWorld,工程右键->Properties->Java build path->Libraries,把mysql connector加进去,connector可以去官网下载,最新版本向前兼容。在工程的WebRoot目录下新建jsp文件,完成后deploy,启动Myeclipse自带的tomcat服务器,进行测试。

工程访问路径的配置(有点坑爹):工程右键->Properties->MyEclipse->Web->Web Context-root

从别个网站转来几个示例程序(http://www.blogjava.net/jing-er/archive/2007/04/19/111733.html):

案例1 :displace.jsp
<%@   page   contentType="text/html;   charset=GB2312"   %>   
  <form   name="form1"   action="displace.jsp"   method="post">   
  <br><br>   
  <input   type="text"   name="Vals"><input   type="text"   name="Amount">   
  <input   type="submit"   name="Submit"   value="Submit">   
  </form>   
  <%   
  int   intLocal_Vals,   intLocal_Amount;   
  if(request.getParameter("Vals")!=null   &&   request.getParameter("Amount")!=null)   
  {   
      intLocal_Vals   =   Integer.parseInt(request.getParameter("Vals"));   
      intLocal_Amount   =   Integer.parseInt(request.getParameter("Amount"));   
      //下面进行位移操作   
    intLocal_Vals=intLocal_Vals>>intLocal_Amount;   
      out.print("<br>位移后的值为:"   +intLocal_Vals);   
  }else{   
      out.print("位移值或位移量不能为空!");   
  }   
  %> 
案例1的所有操作都在一个页面内完成,一般不会出现什么问题,主要用来认识一下jsp页面的组成结构。

案例2 :准备工作:在d:盘建立一个名为count.txt的空文本文档。
<%@ page language="java" contentType="text/html; charset=gb2312"%>

<html>
<head>

<title>文字计数器</title>
</head>
<body bgcolor="#ffffff">
<%@page import="java.io.*" %>
<% 
BufferedReader file;
//BufferedReader 对象用于读取文件数据
String countFile="d:/count.txt";
//标示文件的地址
file=new BufferedReader(new FileReader(countFile));
//将file(BufferedRead的对象)指向文件的地址
String readStr=null;
//来存取文件的内容
int writeStr=1;
//写入文件的变量 如果文件中访问是0 则写入为1
try
{
    readStr=file.readLine();//读取文件内容
    }
catch(IOException e){
    System.out.println("read wrong");
    }
if(readStr==null) readStr="no record";
else {
    writeStr=Integer.parseInt(readStr)+1;//读取的内容+1
    }
try{
    PrintWriter pw;
    //PrintWriter用于写文件的一个类
    pw=new PrintWriter(new FileOutputStream(countFile));
    //指定文件
    pw.println(writeStr);
    //写入变量writeStr的值
    pw.close();
}
catch(IOException e){
    out.println(e.getMessage());
}
%>
<p align="center">
<h1>文字计数器</h1>
<h3>你是本站第</h3>
<font color="ff0000" size="7">
<%=readStr%></font>
<h3>个读者</h3>
</body>
</html>
案例2主要是和外部文件进行了简单的通讯,用到的主要是java代码。

案例3:准备工作:安装mysql;将mysql的JDBC驱动器拷贝到Tomcat\common\lib和Tomcat\shared\lib 下。
<%@ page contentType="text/html;charset=GB2312" %> 
<%@ page language="java" import="java.sql.*"%> 
<% 
Connection conn = null; //连接
Class.forName("org.gjt.mm.mysql.Driver"); //驱动
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","8"); //建立连接
if(conn==null){ 
out.println("get Conn Error"); 

Statement stmt=conn.createStatement(); 
ResultSet RS_result=null; 
%> 
<html> 
<head> 
<title>学习</title></head> 
<body> 
<% 
RS_result=stmt.executeQuery("select * from user"); 
String Name,Password; 
while(RS_result.next()) 

Name=RS_result.getString("name");
Password=RS_result.getString("password"); 
%> 
<P><%=Name%>
<%=Password%></p>
<% 
}

stmt.close(); 
conn.close(); 
%> 
</body> 
</html>
案例3里其实只是用java实现了一个数据库连接。

案例4:
login.jsp
<%@   page   contentType="text/html;   charset=GB2312"   %> 
<html>
  <head>
   <title>login</title>
  </head>
  
  <body>
    <form name="Sayhi" method="post" action="Jsp2.jsp">
    <p>请输入用户信息:</p>
   <p>姓名 <input type="text" name="name" size="12"></p>
   <p>密码 <input type="password" name="password" size="12"></p>
    <input type="submit" value="确认">
    <input type="reset" value="取消">
</body>
</html>
handle.jsp
<%@page import="java.sql.*" contentType="text/html;charset=gb2312"   %>   
  <html>   
  <head> 
  <title>认证</title>   
  </head>   
  <body>   
  <% 
  String   Name=request.getParameter("name");   
        String   Password=request.getParameter("password");   
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();   
        String   url="jdbc:mysql://localhost:3306/db";   
        String   user="root";   
        String   password="8";   
        Connection   conn=DriverManager.getConnection(url,user,password);   
        Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   
        String   sql="select * from   user   where   name='"+Name+"'   and   password='"+Password+"'";   
        ResultSet   rs=stmt.executeQuery(sql);   
        if(rs.next()){   
        out.print("恭喜你,登陆成功!");   
        }   
        else{   
        out.print("抱歉!登陆不成功!");   
        }   
        rs.close();   
        stmt.close();   
        conn.close();   
        %>   
  </body>   
  </html>
案例4是jsp最常用的功能,实现用户登陆的问题。

案例5:
CountTest.java
package Test;

public class CountTest {
 private static int count = 0;
   
    public CountTest() {
 }
 
    public static int getCount() {
 count++;
 return count;
 }

  public static void setCount(int a) {
count =a;
 }
}
counter.jsp
<%@page import="Test.*"%>

<HTML>
<HEAD>
<TITLE>
counter
</TITLE>
</HEAD>
<BODY>
<H1>
JBuilder Generated JSP
</H1>
<jsp:useBean id="bean0" scope="application" class="Test.CountTest" />
<%
out.println("The Counter is : " + bean0.getCount() + "<BR>");
%>
</BODY>
</HTML>
案例5是在java完成处理,在jsp里完成显示的例子。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值