Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial 100 points for jsp servlet questions. Can you take them all_html/css_WEB-ITnose

100 points for jsp servlet questions. Can you take them all_html/css_WEB-ITnose

Jun 24, 2016 am 11:54 AM

求jsp写的修改个人信息的代码!数据库是access。
<%@page import="bean.UserinfoBean"%>
<%@page import="bean.MessageinfoBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
UserinfoBean userinfo=(UserinfoBean)session.getAttribute("userinfors");
%>
<%
UserinfoBean userinfoBean = (UserinfoBean)request.getSession().getAttribute("userinfoBean");
 %>




 
function IsDigit(cCheck) 

return (('0'<=cCheck) && (cCheck<='9')); 


function IsAlpha(cCheck) 

return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z'))) 


function IsValid() 

var struserName = reg.UserName.value; 
for (nIndex=0; nIndex
cCheck = struserName.charAt(nIndex); 
if (!(IsDigit(cCheck) || IsAlpha(cCheck))) 

return false; 


return true; 

function chkEmail(str) 

return str.search(/[w-]{1,}@[w-]{1,}.[w-]{1,}/)==0?true:false ;


function docheck() 

 if(reg.NickName.value =="") 

alert("昵称不能为空"); 
return false; 

else if(reg.Email.value =="") 

alert("邮箱不能为空"); 
return false; 

else if(!chkEmail(reg.Email.value)) 

alert("请填写有效的Email地址"); 
return false; 

else 

return true; 




td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
line-height: 24px;
color: #333333;
}

img {
background-repeat: none;
background-position: center;
}



/image/content.jpg">

         尊敬的<%=userinfo.user_nicheng %>请修改个人资料

action="<%=request.getContextPath()%>/servlet/lybControServlet"
method="post" onSubmit="docheck()">


face="Arial, Helvetica, sans-serif">用户名:
name="UserName" readonly value="<%=userinfo.user_name %>"> value="edituser"> 
 



请修改昵称:
name="NickName">


请修改性别:
name="Sex" value="0" checked>男  name="Sex" value="1">女


请修改Email地址:
name="Email">



  name="res" value="重填">



返回






这是前台的代码,后台方法怎么写?


回复讨论(解决方案)

我的问题 难还是?

public void changeUserInfo(HttpServletRequest req, HttpServletResponse res)  throws IOException, SQLException{
int user_id = Integer.parseInt(req.getParameter("user_id"));

     
     UserinfoBean userinfoBean = new UserinfoBean();
     userinfoBean.setUser_name("");
     userinfoBean.setUser_nicheng("");
     userinfoBean.setUser_sex("");
     userinfoBean.setUser_mail("");
     userinfoBean.setUser_id(user_id);
    HttpSession session =  req.getSession() ; 
    session.setAttribute("userinfoBean", userinfoBean); 
    res.sendRedirect(req.getContextPath() "/jsp/mofy.jsp");
}
这个方法我已经能调用了  但是没有写,不会  菜鸟,求大神高数我怎么写

在docheck()方法内实现写入数据库就可以。你看看用struts框架怎么搞吧

求更改代码码,struts框架没用过

给你写了一个简单的,

首先真的很简单,是W7 mysql5.5 servlet

mysql的sql语句:

create database person;use personcreate table student(    id int not null auto_increment,    username varchar(100),    name varchar(50),    sex varchar(10),    email varchar(50),   primary key(id));-- 插入一条测试数据insert into student(username,name,sex,email)values('test123','fdsaas','1','fdsafkldjsklfds@qq.com');
Copy after login


------------------

下面是Java部分:

DB类[时间仓促,可改进地方很多,自己慢慢练吧]

package test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;/** * Created by prd on 2014/11/6. */public class DB {    private Connection conn = null;    /**     * 获取连接     *     * @return     * @throws java.sql.SQLException     */    public Connection getConn() {        try {            Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/person", "root", "123456");            return conn;        } catch (Exception e) {            e.printStackTrace();        }        return conn;    }    /**     * 关闭连接     *     * @throws SQLException     */    public void closeConn() {        try {            if (conn != null && conn.isClosed()) {                conn.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }    /**     * 更新数据.     * <p/>     * 数组分别是:username,name,sex,email,     * id为固定.可动态修改.     *     * @param params     * @return     */    public int update(String[] params) {        conn = getConn();        String sql = " update student set username='" + params[0] + "',name='" + params[1] + "',sex='" + params[2] + "',email='" + params[3] + "'  where id=1 ";        Statement st = null;        int count = 0;        try {            st = conn.createStatement();            count = st.executeUpdate(sql);        } catch (SQLException e) {            e.printStackTrace();        }        closeConn();        return count;    }}
Copy after login


下面是servlet类:

package test;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * Created by prd on 2014/11/6. */public class UpdateUserServlet extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        String name = request.getParameter("name");        String username = request.getParameter("username");        String sex = request.getParameter("sex");        String email = request.getParameter("email");        if(sex.equals("0"))        {            sex="男";        }else        {            sex="女";        }        DB db = new DB();        int count =db.update(new String[]{username,name,sex,email});        if(count>0)        {            System.out.println("更新成功");        }else        {            System.out.println("更新失败");        }    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }}
Copy after login


下面是jsp页面[ 替换你原来的表单部分]:

<form name="reg"          action="<%=request.getContextPath()%>/servlet/UpdateUserServlet"          method="post" onSubmit="docheck()">        <table width="90%" border="0">            <tr>                <td width="50%" align="right" height="25"><font                        face="Arial, Helvetica, sans-serif">用户名:</font></td>                <td width="50%" align="left" height="25"><input type="text"                                                                name="username" value=""> <input type="hidden" name="method"                                                                                                                                  value="edituser"> <br>                </td>            </tr>            <tr>                <td width="50%" align="right" height="25">请修改昵称:</td>                <td width="50%" align="left" height="25"><input type="text"                                                                name="name"></td>            </tr>            <tr>                <td width="50%" align="right" height="25">请修改性别:</td>                <td width="50%" align="left" height="25"><input type="radio"h                                                                name="sex" value="0" checked>男 <input type="radio"                                                                                                      name="sex" value="1">女</td>            </tr>            <tr>                <td width="50%" align="right" height="25">请修改Email地址:</td>                <td width="50%" align="left" height="25"><input type="text"                                                                name="email"></td>            </tr>        </table>        <p>            <input type="submit" name="sub" value="更改"> <input type="reset"                                                               name="res" value="重填">        </p>        <p>            <a href="index.jsp">返回</a>        </p>    </form>
Copy after login


下面是web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <servlet>        <servlet-name>UpdateUserServlet</servlet-name>        <servlet-class>test.UpdateUserServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>UpdateUserServlet</servlet-name>        <url-pattern>/servlet/UpdateUserServlet</url-pattern>    </servlet-mapping></web-app>
Copy after login


记得一定要导入mysql的驱动包,直接百度搜索java连接mysql驱动包就可以,加入到项目里面。

如果你要用access,改掉这两句就可以了.

 Class.forName("com.mysql.jdbc.Driver");// 加载Access数据的驱动            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/person", "root", "123456");//连接地址
Copy after login

sql code这个放到那里,表已经有了,叫userinfo,而且那个db  最下面的更新数据我怎么加到我的db里面啊

大神,完全跟你写的不一样,我已经打私信给你了,你打开加我qq远成协助一下你就知道怎么回事了

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

See all articles