本文目录一览:
需要一个可以运行的JSP简单代码?
%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%
%
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%"
titleMy JSP 'index.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"
--
/head
body
This is my JSP page. br
/body
/html
jsp代码问题
% Vector listclass = new Vector(); //定义个Vector容器对象
DispClass dispclass = new DispClass(); //定义一个DispClass类
listclass = dispclass.allClass(); //把dispclass.allClass();返回的对象赋值给listclass
for(int i=0;ilistclass.size();i++) {
//从容器从取得每一个IClass类型的对象,
IClass classinfo = (IClass)listclass.elementAt(i);%
//然后把id赋值给option的value 名字赋值给option的实际值
option value="%=classinfo.getID()%"%=classinfo.getName()%/option
%}%1.a.jsp提交到b.jsp,
b.jsp里面这样写:
String userName=request.getParameter("userName");//页面表单的值
request.getSession().setAttribute("userName",userName);//加入session
2.在c.jsp或者其他jsp这样写:
String userName=(String)request.getSession().getAttribute("userName");//取值
注意getAttribute()里面用双引号括起来的部分要和setAttribute()里面用双引号括起来的地方一样。
求大神写一下jsp的简单的注册界面代码。
1.需要一个jsp页面:
//login.jsp核心代码:
form action="${pageContext.request.contextPath}/servlet/UserServlet" method="post"
input type="text" name="loginname" /input type="password" name="password"/
input type="submit" value="登录"/
/form
2.需要一个servlet来验证登录信息
//UserServlet 核心代码
class UserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String loginname = request.getParameter("loginname");
String password = request.getParameter("password");
//创建一个service来处理业务逻辑(包括查询数据库操作)
UserService service = new UserService();
boolean bool = service.validateUser(loginname,password);
if(!bool){
pw.println("用户名或密码错误");
}else{
pw.println("登录成功");
}
}
3.需要一个service处理业务逻辑(包括查询数据库操作)
//UserService 核心代码
public class UserService{
/**
*查询数据库验证用户是否存在,返回boolean
*/
public boolean validateUser(String loginname,String password){
boolean bool = false;
Connection conn = null;
PreparedStatement ps = null;
//这里以mysql为例
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
String sql = "select login_name,pass_word from t_user where login_name=? and pass_word=?";
ps = conn.prepareStatement(sql);
ps.setString(0, loginname);
ps.setString(1, password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
bool = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(conn != null){
conn.close();
conn = null;
}
if(ps != null){
ps.close();
ps = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return bool;
}
}
简单jsp代码问题
完整jsp页面如下:
%@ page import="java.util.*" %
%@ page contentType="text/html; charset=gb2312" %
html
head
Title欢迎您进入JSP世界!/Title
/head
body
center
%
for(int i=0;i10;i++)
out.println("Hello,JSP World!"+ "br/");
%
/body
/html